采用 QT5.15 VTK9.2.0
建立QT QUICK项目
部分方法来源于 QML加载VTK
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickVTKRenderWindow.h>
#include <QQuickVTKRenderItem.h>
#include <vtkPolyDataMapper.h>
#include <vtkCylinderSource.h>
#include <vtkCubeSource.h>
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
//定义app之前调用一次, 官方文档上写的
QQuickVTKRenderWindow::setupGraphicsBackend();
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterType<QQuickVTKRenderWindow>("VTK", 9, 2, "VTKRenderWindow");
qmlRegisterType<QQuickVTKRenderItem>("VTK", 9, 2, "VTKRenderItem");
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
//------------------------------------
//建立一个圆柱体
vtkNew<vtkCylinderSource> cylinder;
cylinder->SetResolution(8);
vtkNew<vtkPolyDataMapper> cylinderMapper;
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
vtkSmartPointer<vtkActor> cylinderActor=vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);
auto qquickvtkItem = engine.rootObjects().first()
->findChild< QQuickVTKRenderItem* >( "ConeView" );
auto main_render_3d = qquickvtkItem->renderer();
main_render_3d->AddActor(cylinderActor);
main_render_3d->ResetCamera();
QQuickVTKRenderWindow* ren_win_3d = engine.rootObjects().first()
->findChild< QQuickVTKRenderWindow* >( "renderWindow" );
ren_win_3d->render();
return app.exec();
}
main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import VTK 9.2
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
onWidthChanged: { console.log(width) }
//---------------------------------------
//---------------------------------------
VTKRenderWindow{
id: vtkwindow
z: 10
//注意这个objectName, 等会要用到!!!!! x1
objectName: "renderWindow"
width: parent.width * 0.9
height: width
anchors.centerIn: parent
}
VTKRenderItem{
id: coneViewItem
z: 10
//注意这个objectName, 等会要用到!!!!! x2
objectName: "ConeView"
anchors.fill: vtkwindow
renderWindow: vtkwindow
focus: true
}
}
pro文件
QT += quick
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
##VTK
INCLUDEPATH+=$$quote(D:/WORKSPACE/VTK/VTK920/runtime/include/vtk-9.2)
CONFIG(debug,debug|release){
LIBS+=$$quote(D:/WORKSPACE/VTK/VTK920/runtime/lib/*.lib)
}else{
LIBS+=$$quote(D:/WORKSPACE/VTK/VTK920/runtime/lib/*.lib)
}