本文主要是介绍QML配合VTK基本实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
采用 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.5import VTK 9.2Window {width: 640height: 480visible: truetitle: qsTr("Hello World")onWidthChanged: { console.log(width) }//---------------------------------------//---------------------------------------VTKRenderWindow{id: vtkwindowz: 10//注意这个objectName, 等会要用到!!!!! x1objectName: "renderWindow"width: parent.width * 0.9height: widthanchors.centerIn: parent}VTKRenderItem{id: coneViewItemz: 10//注意这个objectName, 等会要用到!!!!! x2objectName: "ConeView"anchors.fill: vtkwindowrenderWindow: vtkwindowfocus: 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.0SOURCES += \main.cppRESOURCES += 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)
}
这篇关于QML配合VTK基本实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!