知识不是单独的,一定是成体系的。更多我的个人总结和相关经验可查阅这个专栏:Visual Studio。
关于更多此例子的资料,可以参考:【Visual Studio】在 Windows 上使用 Visual Studio 配合 Qt 构建 VTK。
文章目录
- 版本环境
- `VTKTest.ui`
- `VTKTest.h`
- `VTKTest.cpp`
- `main.cpp`
- 运行结果
- Ref.
版本环境
版本环境为:
- 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();
}
运行结果
Ref.
- QVTKOpenGLNativeWidget在Qt中的用法
- VTK+Qt的第一个例子