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. qmlsc compilation fails for qmlRegisterSingletonInstance
Forum Updated to NodeBB v4.3 + New Features

qmlsc compilation fails for qmlRegisterSingletonInstance

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 448 Views 1 Watching
  • 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.
  • R Offline
    R Offline
    robgu
    wrote on last edited by
    #1

    Hello,

    Recently I've been testing the qmlsc compilation. I'm currently facing a problem of a piece of QML code not being compiled by qmlsc. Note, that when executed it still works, but I want to find out how to do it "properly".

    I need to share a c++ side created instance of an object with QML, so I followed the usage given for QQmlEngine::qmlRegisterSingletonInstance.
    For that I created a simple project (all source files below) that has a simple DummyData class storing some text. An instance of that class is created in the main (before creating QQmlEngine) and registered using
    qmlRegisterSingletonInstance with URI "MyTest.Test.Dummy", version 1.0, and type name "DD". I then import the "MyTest.Test.Dummy 1.0" in the qml and call for DD properties.
    This works fine, however, when compiling the QML code I get the following 3 warnings (which, AFAIK, prevent the full qml to cpp compilation):

    [10/19 1,4/sec] Generating .rcc/qmlcache/appSingletonTest_Main_qml.cpp
    Warning: Main.qml:2:1: Warnings occurred while importing module "MyTest.Test.Dummy": [import]
    import MyTest.Test.Dummy 1.0
    ^^^^^^
    ---
    Warning: Failed to import MyTest.Test.Dummy. Are your import paths set up properly? [import]
    ---
    
    Warning: Main.qml:13:15: Unqualified access [unqualified]
            text: DD.dummyText
                  ^^
    Warning: Main.qml:13:15: Could not compile binding for text: Cannot access value for name DD [compiler]
            text: DD.dummyText
                  ^^
    

    How can I resolve these warnings and take advantage of the full QML compilation?
    FYI: I'm using Qt6.5.5

    Source files:
    DummyData.h

    #ifndef DUMMYDATA_H
    #define DUMMYDATA_H
    
    #include <QObject>
    
    class DummyData : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString dummyText READ dummyText WRITE setDummyText NOTIFY dummyTextChanged FINAL)
    
    public:
        explicit DummyData(QObject *parent = nullptr);
    
        QString dummyText() const;
        void setDummyText(const QString &newDummyText);
    
    signals:
        void dummyTextChanged();
    
    private:
        QString m_dummyText;
    };
    
    #endif // DUMMYDATA_H
    

    DummyData.cpp

    #include "DummyData.h"
    
    DummyData::DummyData(QObject *parent)
        : QObject{parent}
    {}
    
    QString DummyData::dummyText() const
    {
        return m_dummyText;
    }
    
    void DummyData::setDummyText(const QString &newDummyText)
    {
        if (m_dummyText == newDummyText)
            return;
        m_dummyText = newDummyText;
        emit dummyTextChanged();
    }
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "DummyData.h"
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QScopedPointer<DummyData> dd(new DummyData);
        dd->setDummyText("my test text");
        qmlRegisterSingletonInstance("MyTest.Test.Dummy", 1, 0, "DD", dd.get());
    
        QQmlApplicationEngine engine;
        QObject::connect(
            &engine,
            &QQmlApplicationEngine::objectCreationFailed,
            &app,
            []() { QCoreApplication::exit(-1); },
            Qt::QueuedConnection);
        engine.loadFromModule("SingletonTest", "Main");
    
        return app.exec();
    }
    

    Main.qml

    import QtQuick
    import MyTest.Test.Dummy 1.0
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Text {
            id: text1
            anchors.centerIn: parent
            text: DD.dummyText
        }
    }
    

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.16)
    
    project(SingletonTest VERSION 0.1 LANGUAGES CXX)
    
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
    
    qt_standard_project_setup(REQUIRES 6.5)
    
    qt_policy(SET QTP0001 NEW)
    
    qt_add_executable(appSingletonTest
        main.cpp
    )
    
    qt_add_qml_module(appSingletonTest
        URI SingletonTest
        VERSION 1.0
        QML_FILES
            Main.qml
            SOURCES DummyData.h DummyData.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(appSingletonTest PROPERTIES
    #    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appSingletonTest
        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(appSingletonTest
        PRIVATE Qt6::Quick
    )
    
    include(GNUInstallDirs)
    install(TARGETS appSingletonTest
        BUNDLE DESTINATION .
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    )
    
    1 Reply Last reply
    0
    • R Offline
      R Offline
      robgu
      wrote on last edited by
      #3

      Yes and no.
      No: I did not solve it the way I have imagined it.
      Yes: I changed DummyData to be QML_SINGLETON and use QQmlEngine::singletonInstance to get the pointer to the instance in the C++ code.

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

        Did you solve it?

        1 Reply Last reply
        0
        • R Offline
          R Offline
          robgu
          wrote on last edited by
          #3

          Yes and no.
          No: I did not solve it the way I have imagined it.
          Yes: I changed DummyData to be QML_SINGLETON and use QQmlEngine::singletonInstance to get the pointer to the instance in the C++ code.

          1 Reply Last reply
          0
          • R robgu has marked this topic as solved on

          • Login

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