I wanted to use QtQuick + QML as the frontend and VTK as the backend.
I found out that VTK 9.1 has implemented QQmlVTKPlugin. So I would like to try it.
See https://vtk.org/doc/nightly/html/classQQmlVTKPlugin.html#details
So I did the following:
- Build VTK 9.1 from source with all Qt support togged on.
- The build was successful and I got the QML module file.
- I just follow the example in https://vtk.org/doc/nightly/html/classQQmlVTKPlugin.html#details
main.qml
// import related modules
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
// import the VTK module
import VTK 9.1
// window containing the application
ApplicationWindow {
// title of the application
title: qsTr("VTK QtQuick App")
width: 400
height: 400
color: palette.window
SystemPalette {
id: palette
colorGroup: SystemPalette.Active
}
// Instantiate the vtk render window
VTKRenderWindow {
id: vtkwindow
width: 400
height: 400
}
// add one or more vtk render items
VTKRenderItem {
objectName: "ConeView"
x: 200
y: 200
width: 200
height: 200
// Provide the handle to the render window
renderWindow: vtkwindow
}
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <qquickwindow.h>
#include <QQuickVTKRenderWindow.h>
#include <QQuickVTKRenderItem.h>
#include <vtkPolyDataMapper.h>
#include <vtkConeSource.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkCylinderSource.h>
#include <vtkRenderWindowInteractor.h>
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QQuickVTKRenderWindow::setupGraphicsBackend();
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.addImportPath("../vendor/VTK_debug_static/QML/Debug");
engine.load(QUrl("qrc:///main.qml"));
QObject* topLevel = engine.rootObjects().value(0);
QQuickWindow* window = qobject_cast<QQuickWindow*>(topLevel);
window->show();
// Fetch the QQuick window using the standard object name set up in the constructor
QQuickItem* item = topLevel->findChild<QQuickItem*>("ConeView");
QQuickVTKRenderItem* qquickvtkItem = static_cast<QQuickVTKRenderItem*>(item);
// Create a cone pipeline and add it to the view
vtkNew<vtkActor> actor;
vtkNew<vtkPolyDataMapper> mapper;
vtkNew<vtkConeSource> cone;
mapper->SetInputConnection(cone->GetOutputPort());
actor->SetMapper(mapper);
// qquickvtkItem->renderer()->AddActor(actor); // why adding actor would cause crash?
qquickvtkItem->renderer()->ResetCamera();
qquickvtkItem->renderer()->SetBackground(0.5, 0.5, 0.7);
qquickvtkItem->renderer()->SetBackground2(0.7, 0.7, 0.7);
qquickvtkItem->renderer()->SetGradientBackground(true);
qquickvtkItem->update();
app.exec();
}
If I uncomment qquickvtkItem->renderer()->AddActor(actor)
, the app is running and I can see the background color. But add it back would cause app to crash.
The error message is
I am not sure what I am missing. Any help would be greatly appreciated!
Thanks
Cam