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. QMLLS not finding manual C++ registrations
QtWS25 Last Chance

QMLLS not finding manual C++ registrations

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlqmllscppregistrationclass
4 Posts 3 Posters 406 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.
  • S Offline
    S Offline
    SimpleY
    wrote on 23 Oct 2024, 21:33 last edited by
    #1

    The issue

    I have a very basic Qt Quick App which builds and runs without issues. However importing QML modules which are manually first registered at runtime by my C++ code in main.cpp are obviously not identified by the QMLLS and therefore I get false warnings for importing the module and whenever I access something from it. This makes QML development with C++ classes/instances registered at runtime quite painful since I don't get auto-completion in the editor.

    Here is Main.qml and the project structure on the left:
    1bb1eb2b-3ddf-4db5-850b-95b5a0bf0b6c-image.png

    CMakeLists.txt:

    cmake_minimum_required(VERSION 3.16)
    
    project(SongPlayer VERSION 0.1 LANGUAGES CXX)
    
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(QT_QML_GENERATE_QMLLS_INI ON)
    
    find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
    
    qt_standard_project_setup(REQUIRES 6.5)
    
    qt_add_executable(appSongPlayer
        main.cpp
    )
    
    qt_add_qml_module(appSongPlayer
        URI SongPlayer
        VERSION 1.0
        QML_FILES
            Main.qml
    
        SOURCES
            TestClass.h TestClass.cpp
    
        NO_CACHEGEN
    )
    
    # 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(appSongPlayer PROPERTIES
    #    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appSongPlayer
        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(appSongPlayer
        PRIVATE Qt6::Quick
    )
    
    include(GNUInstallDirs)
    install(TARGETS appSongPlayer
        BUNDLE DESTINATION .
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    )
    

    main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "TestClass.h"
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        qmlRegisterSingletonInstance("MyTestModule", 1, 0, "TestInstance", new TestClass(&app));
    
        QObject::connect(
            &engine,
            &QQmlApplicationEngine::objectCreationFailed,
            &app,
            []() { QCoreApplication::exit(-1); },
            Qt::QueuedConnection);
        engine.loadFromModule("SongPlayer", "Main");
    
        return app.exec();
    }
    

    TestClass.h:

    #ifndef TESTCLASS_H
    #define TESTCLASS_H
    
    #include <QObject>
    #include <qqml.h>
    
    using namespace Qt::Literals::StringLiterals;
    
    class TestClass : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(QString myText READ getMyText NOTIFY myTextChanged)
    
    public:
        explicit TestClass(QObject *parent = nullptr);
    
        QString getMyText() const { return u"Hello QMLSS"_s; }
    
    signals:
        void myTextChanged();
    
    private:
    };
    
    #endif // TESTCLASS_H
    

    Proof those are false warnings and the app builds:
    a22b74ed-a9fc-4138-ae66-103bd2541e7f-image.png

    Question

    What's the best way to inform the QMLLS of my module (and classes in it) if it's only registered from C++ code as seen here with qmlRegisterSingletonInstance (or in theory with QQmlContext::setContextProperty) for smooth QML development in Qt Creator?

    Environment

    • Windows 10 64-bit
    • Qt Creator 14.0.2 (Community)
    • Qt 6.8.0
    • Project: Qt Quick Application, Kit: Destkop Qt 6.8.0 MinGW 64-bit with CMake
    1 Reply Last reply
    0
    • E Offline
      E Offline
      ekkescorner
      Qt Champions 2016
      wrote on 24 Oct 2024, 13:21 last edited by
      #4

      instead of setContextProperty() you should use QML_SINGLETON
      see: https://t1p.de/ekkeQML_SINGLETON

      ekke ... Qt Champion 2016 | 2024 ... mobile business apps
      5.15 --> 6.8 https://t1p.de/ekkeChecklist
      QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

      1 Reply Last reply
      2
      • A Offline
        A Offline
        Axel Spoerl
        Moderators
        wrote on 24 Oct 2024, 02:57 last edited by
        #2

        Hello again :-)

        Check this documentation, please.
        The QML_ELEMENT macro is missing in TestClass.

        Software Engineer
        The Qt Company, Oslo

        S 1 Reply Last reply 24 Oct 2024, 10:59
        2
        • A Axel Spoerl
          24 Oct 2024, 02:57

          Hello again :-)

          Check this documentation, please.
          The QML_ELEMENT macro is missing in TestClass.

          S Offline
          S Offline
          SimpleY
          wrote on 24 Oct 2024, 10:59 last edited by
          #3

          @Axel-Spoerl Hi again too. I attempted to add QML_ELEMENT previously and that didn't help but to make sure I did not mess something up I just tried again. Here is also proof that it's not some kind of weird cache or that it gets fixed after building:
          https://streamable.com/61he1v

          1 Reply Last reply
          0
          • S SimpleY referenced this topic on 24 Oct 2024, 11:12
          • E Offline
            E Offline
            ekkescorner
            Qt Champions 2016
            wrote on 24 Oct 2024, 13:21 last edited by
            #4

            instead of setContextProperty() you should use QML_SINGLETON
            see: https://t1p.de/ekkeQML_SINGLETON

            ekke ... Qt Champion 2016 | 2024 ... mobile business apps
            5.15 --> 6.8 https://t1p.de/ekkeChecklist
            QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

            1 Reply Last reply
            2
            • S SimpleY has marked this topic as solved on 24 Oct 2024, 20:45
            • S SimpleY has marked this topic as solved on 25 Oct 2024, 11:22

            4/4

            24 Oct 2024, 13:21

            • Login

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