知识不是单独的,一定是成体系的。更多我的个人总结和相关经验可查阅这个专栏:Visual Studio。
关于更多此例子的资料,可以参考:【Visual Studio】在 Windows 上使用 Visual Studio 配合 Qt 构建 VTK。
完成了使用 Qt 来显示一个小球的例子后,我还尝试从 VTK 官网上下载了一个例子,然后自己尝试着将其移植到了自己的 Qt 工程中,感兴趣可以看:【VTK】官方示例,移植到自己的 Qt 工程,含代码。
文章目录
版本环境
版本环境为:
- win11
- visual studio 2022
- VTK-9.2.6
- CMake 3.26.3
- Qt 6.2.8
VTKTest.ui
VTKTest.h
cpp
#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
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
cpp
#include "VTKTest.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
VTKTest w;
w.show();
return a.exec();
}