I have the same problem.
My environment variables:
VTK_DIR=C:/dev/github/VTK/build
QML_IMPORT_PATH=C:/dev/github/VTK/build/lib/qml/Debug
QT_DEBUG_PLUGINS=1
QML_IMPORT_TRACE=1
PATH=C:/dev/github/VTK/build/bin/Debug
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(SphereView VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(QML_IMPORT_PATH C:/dev/github/VTK/build/lib/qml/${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)
find_package(VTK REQUIRED COMPONENTS
GUISupportQtQuick
)
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
qt_standard_project_setup(REQUIRES 6.5)
qt_add_executable(appSphereView
main.cpp
)
qt_add_qml_module(appSphereView
URI SphereView
VERSION 1.0
QML_FILES
Main.qml
SOURCES vtkitem.h vtkitem.cpp
)
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(appSphereView PROPERTIES
# MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appSphereView
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
target_link_libraries(appSphereView
PRIVATE Qt6::Quick
${VTK_LIBRARIES}
)
include(GNUInstallDirs)
install(TARGETS appSphereView
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
Main.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
import VTK 9.4
import com.example.sphereview 1.0
ApplicationWindow {
visible: true
width: 800
height: 600
title: qsTr("VTK Sphere App")
color: palette.window
SystemPalette {
id: palette
colorGroup: SystemPalette.Active
}
VTKRenderWindow {
id: vtkwindow
width: 800
height: 600
}
VtkItem {
objectName: "SphereView"
anchors.fill: parent
renderWindow: vtkwindow
}
}
vtkitem.h
// vtkitem.h
#ifndef VTKITEM_H
#define VTKITEM_H
#include <QQuickVTKRenderItem.h>
#include <vtkActor.h>
#include <vtkPolyDataMapper.h>
#include <vtkSphereSource.h>
#include <vtkNew.h>
class VtkItem : public QQuickVTKRenderItem
{
Q_OBJECT
public:
VtkItem(QQuickItem* parent = nullptr);
private:
vtkNew<vtkActor> actor;
vtkNew<vtkPolyDataMapper> mapper;
vtkNew<vtkSphereSource> sphere;
};
#endif // VTKITEM_H
vtkitem.cpp
// vtkitem.cpp
#include "vtkitem.h"
VtkItem::VtkItem(QQuickItem* parent)
: QQuickVTKRenderItem(parent)
{
sphere->SetRadius(5.0);
mapper->SetInputConnection(sphere->GetOutputPort());
actor->SetMapper(mapper);
renderer()->AddActor(actor);
renderer()->ResetCamera();
renderer()->SetBackground(0.5, 0.5, 0.7);
renderer()->SetBackground2(0.7, 0.7, 0.7);
renderer()->SetGradientBackground(true);
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <vtkitem.h>
int main(int argc, char *argv[])
{
QQuickVTKRenderWindow::setupGraphicsBackend();
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
// Register the custom VTK item with QML
qmlRegisterType<VtkItem>("com.example.sphereview", 1.0, 0, "VtkItem");
QObject::connect(
&engine,
&QQmlApplicationEngine::objectCreationFailed,
&app,
[]() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.loadFromModule("SphereView", "Main");
return app.exec();
}
5dffe8ce-ee5f-4e99-80f6-b04c75f14e34-image.png
fd48d1a7-e90b-4b61-a276-c6e7bf0d29b2-image.png