Knowledge is not alone, it must be systematic. More my personal summary and related experience can be found in this column: Visual Studio .
For more information about this example, please refer to: 【Visual Studio】Use Visual Studio and Qt to build VTK on Windows .
After completing the example of using Qt to display a small ball, I also tried to download an example from the VTK official website, and then tried to port it to my own Qt project. If you are interested, you can see: [VTK] Official example, ported to my own Qt project, including code .
Article Directory
version environment
The version environment is:
- win11
- visual studio 2022
- VTK-9.2.6
- CMake 3.26.3
- Qt 6.2.8
VTKTest.ui
VTKTest.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_VTKTest.h"
#include <qsurfaceformat.h>
#include <QVTKOpenGLNativeWidget.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include "vtkAutoInit.h"
class VTKTest : public QMainWindow
{
Q_OBJECT
public:
VTKTest(QWidget* parent = nullptr);
//VTKTest(QWidget* parent = Q_NULLPTR);
~VTKTest();
private slots:
void on_pushButton_clicked();
private:
Ui::VTKTestClass ui;
};
VTKTest.cpp
#include "VTKTest.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTKTest::VTKTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
VTKTest::~VTKTest()
{
}
void VTKTest::on_pushButton_clicked()
{
QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
//QVTKOpenGLNativeWidget* widget = new QVTKOpenGLNativeWidget();
vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();
vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
sphereActor->SetMapper(sphereMapper);
sphereActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(sphereActor);
renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());
vtkSmartPointer<vtkGenericOpenGLRenderWindow> renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("RenderWindowNoUIFile");
ui.qvtkWidget->setRenderWindow(renderWindow);
ui.qvtkWidget->resize(200, 160);
ui.qvtkWidget->show();
}
main.cpp
#include "VTKTest.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
VTKTest w;
w.show();
return a.exec();
}