Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. VTK not being recognized in QML
Forum Updated to NodeBB v4.3 + New Features

VTK not being recognized in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 677 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amirreza Taghizadeh
    wrote on last edited by
    #1

    Hello,

    I've been struggling to include VTK into QML. I have installed VTK 9.3 through ninja, following the Kitware's gitlab docs. I also have set env variables QML_IMPORT_PATH and QML_IMPORT_PATH to VTKInstallation/lib/qml/vtk-9.3. Everything seems to be working, except QTCreator not identifying VTK qml Components

    Here is my CmakeLists.txt:

    cmake_minimum_required(VERSION 3.16)
    
    project(hii VERSION 0.1 LANGUAGES CXX)
    
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    find_package(Qt6 6.5 REQUIRED COMPONENTS Quick OpenGL)
    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})
    
    qt_standard_project_setup(REQUIRES 6.5)
    
    qt_add_executable(apphii
        main.cpp
    )
    qt_add_qml_module(apphii
        URI hii
        VERSION 1.0
        QML_FILES
            Main.qml
    )
    set_target_properties(apphii PROPERTIES
        WIN32_EXECUTABLE TRUE
    )
    target_link_libraries(apphii
        PRIVATE Qt6::Quick
        ${QT_LIBRARIES}
        ${OPENGL_LIBRARIES}
        ${VTK_LIBRARIES}
    )
    include(GNUInstallDirs)
    install(TARGETS apphii
        BUNDLE DESTINATION .
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    )
    

    Now, my Main.qml contains these VTK lines:

    import VTK 9.3  # imports just fine!
    ...
    VTKRenderWindow { # Unknown Component (M300)
          id: vtkwindow
          width: 400
          height: 400
        }
    

    The code runs without any problems, nonetheless the qtcreator doesn't seem to recognize the module's components.
    BTW, the directory VTKInstallation/lib/qml/vtk-9.3 contains qmldir and plugins.qmltypes:
    qmldir file:

    import QtQuick.tooling 1.2
    
    // This file describes the plugin-supplied types contained in the library.
    // It is used for QML tooling purposes only.
    //
    // This file was auto-generated by:
    // 'qmlplugindump -output $qmldir/VTK.9.1/plugins.qmltypes VTK 9.1 $qmldir'
    
    Module {
        dependencies: ["QtQuick 2.0"]
        Component {
            name: "QQuickVTKInteractiveWidget"
            prototype: "QObject"
            exports: ["VTKWidget 9.1"]
            exportMetaObjectRevisions: [0]
            Property { name: "enabled"; type: "bool" }
            Signal {
                name: "enabledChanged"
                Parameter { name: "e"; type: "bool" }
            }
            Method {
                name: "sync"
                Parameter { name: "ren"; type: "vtkRenderer"; isPointer: true }
            }
        }
        Component {
            name: "QQuickVTKRenderItem"
            defaultProperty: "data"
            prototype: "QQuickItem"
            exports: ["VTKRenderItem 9.1"]
            exportMetaObjectRevisions: [0]
            Property { name: "renderWindow"; type: "QQuickVTKRenderWindow"; isPointer: true }
            Method { name: "sync" }
            Method { name: "init" }
            Method { name: "paint" }
            Method { name: "cleanup" }
        }
        Component {
            name: "QQuickVTKRenderWindow"
            defaultProperty: "data"
            prototype: "QQuickItem"
            exports: ["VTKRenderWindow 9.1"]
            exportMetaObjectRevisions: [0]
            Method { name: "sync" }
            Method { name: "init" }
            Method { name: "paint" }
            Method { name: "cleanup" }
            Method { name: "renderNow" }
            Method { name: "render" }
        }
    }
    

    plugins.qmltypes file:

    import QtQuick.tooling 1.2
    
    // This file describes the plugin-supplied types contained in the library.
    // It is used for QML tooling purposes only.
    //
    // This file was auto-generated by:
    // 'qmlplugindump -output $qmldir/VTK.9.1/plugins.qmltypes VTK 9.1 $qmldir'
    
    Module {
        dependencies: ["QtQuick 2.0"]
        Component {
            name: "QQuickVTKInteractiveWidget"
            prototype: "QObject"
            exports: ["VTKWidget 9.1"]
            exportMetaObjectRevisions: [0]
            Property { name: "enabled"; type: "bool" }
            Signal {
                name: "enabledChanged"
                Parameter { name: "e"; type: "bool" }
            }
            Method {
                name: "sync"
                Parameter { name: "ren"; type: "vtkRenderer"; isPointer: true }
            }
        }
        Component {
            name: "QQuickVTKRenderItem"
            defaultProperty: "data"
            prototype: "QQuickItem"
            exports: ["VTKRenderItem 9.1"]
            exportMetaObjectRevisions: [0]
            Property { name: "renderWindow"; type: "QQuickVTKRenderWindow"; isPointer: true }
            Method { name: "sync" }
            Method { name: "init" }
            Method { name: "paint" }
            Method { name: "cleanup" }
        }
        Component {
            name: "QQuickVTKRenderWindow"
            defaultProperty: "data"
            prototype: "QQuickItem"
            exports: ["VTKRenderWindow 9.1"]
            exportMetaObjectRevisions: [0]
            Method { name: "sync" }
            Method { name: "init" }
            Method { name: "paint" }
            Method { name: "cleanup" }
            Method { name: "renderNow" }
            Method { name: "render" }
        }
    }
    

    Would someone please why QTCreator doesn't know about the components?

    Thank you very much in advance

    Regards

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Anumas
      wrote on last edited by
      #2

      Hmm, have you tried resetting the QML/JS model? Tools -> QML/JS -> Reset Code Model

      Say hello to a bright day.-

      Anumas.

      A 1 Reply Last reply
      0
      • A Anumas

        Hmm, have you tried resetting the QML/JS model? Tools -> QML/JS -> Reset Code Model

        A Offline
        A Offline
        Amirreza Taghizadeh
        wrote on last edited by
        #3

        @Anumas Thanks for the response. I tried it but unfortunately it didn't solve the issue.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Michael Banucu Kapp-Niles
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved