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. ReferenceError to root window id when creating QML from C++

ReferenceError to root window id when creating QML from C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 1.7k 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.
  • C Offline
    C Offline
    ChrisTof
    wrote on last edited by
    #1

    I want to load QML files from within C++.
    The problem is that the main window cannot be referenced by id from a different QML file.
    I create the main window first:

        QGuiApplication  app(argc, argv);
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        QQuickWindow *window = qobject_cast<QQuickWindow*>(engine.rootObjects().at(0));
        if (!window) {
            qFatal("Error: Your root item has to be a window.");
            return;
        }
        window->show();
        root = window->contentItem();
    

    main.qml:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        id: root
        width: 640
        height: 480
    }
    

    Then, I add a rectangle:

    QQmlComponent component(enginePtr, QUrl("qrc:/rec.qml"));
    QQuickItem* object = qobject_cast<QQuickItem*>(component.beginCreate(enginePtr->rootContext()));
    
        if(object == nullptr) 
             return;
    
    QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
    object->setParentItem(root);
    object->setParent(enginePtr);
    component.completeCreate();
    

    rec.qml:

    import QtQuick 2.0
    Rectangle {
    width:100
    color:"red"
        Component.onCompleted: {
            height = root.height/Math.max(sourceSize.height, sourceSize.width) * scale;
        }
    }
    

    As you can see, rectangle height is calculated from root.height, which is MainWindow height. But, this results in the error:

    ReferenceError: root is not defined
    

    How can I refer to the properties of the main window from a different QML file?

    raven-worxR 1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      Do you have a specific reason doing all that in C++?
      It's considered bad practice.

      C 1 Reply Last reply
      0
      • C ChrisTof

        I want to load QML files from within C++.
        The problem is that the main window cannot be referenced by id from a different QML file.
        I create the main window first:

            QGuiApplication  app(argc, argv);
            QQmlApplicationEngine engine;
            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
            QQuickWindow *window = qobject_cast<QQuickWindow*>(engine.rootObjects().at(0));
            if (!window) {
                qFatal("Error: Your root item has to be a window.");
                return;
            }
            window->show();
            root = window->contentItem();
        

        main.qml:

        import QtQuick 2.12
        import QtQuick.Window 2.12
        
        Window {
            id: root
            width: 640
            height: 480
        }
        

        Then, I add a rectangle:

        QQmlComponent component(enginePtr, QUrl("qrc:/rec.qml"));
        QQuickItem* object = qobject_cast<QQuickItem*>(component.beginCreate(enginePtr->rootContext()));
        
            if(object == nullptr) 
                 return;
        
        QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
        object->setParentItem(root);
        object->setParent(enginePtr);
        component.completeCreate();
        

        rec.qml:

        import QtQuick 2.0
        Rectangle {
        width:100
        color:"red"
            Component.onCompleted: {
                height = root.height/Math.max(sourceSize.height, sourceSize.width) * scale;
            }
        }
        

        As you can see, rectangle height is calculated from root.height, which is MainWindow height. But, this results in the error:

        ReferenceError: root is not defined
        

        How can I refer to the properties of the main window from a different QML file?

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @GrecKo said in ReferenceError to root window id when creating QML from C++:

        It's considered bad practice.

        what? why?

        @ChrisTof said in ReferenceError to root window id when creating QML from C++:

        QQuickItem* object = qobject_cast<QQuickItem*>(component.beginCreate(enginePtr->rootContext()));

        you should use the context (or a child context) of the root item, rather the engine's rootContext

        QQmlContext* rootItemContext = QQmlEngine::contextForObject(root);
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        GrecKoG 1 Reply Last reply
        1
        • raven-worxR raven-worx

          @GrecKo said in ReferenceError to root window id when creating QML from C++:

          It's considered bad practice.

          what? why?

          @ChrisTof said in ReferenceError to root window id when creating QML from C++:

          QQuickItem* object = qobject_cast<QQuickItem*>(component.beginCreate(enginePtr->rootContext()));

          you should use the context (or a child context) of the root item, rather the engine's rootContext

          QQmlContext* rootItemContext = QQmlEngine::contextForObject(root);
          
          GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          @raven-worx said in ReferenceError to root window id when creating QML from C++:

          what? why?

          Instead of paraphrasing, I'll copy those links:

          • https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
          • http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
          • https://youtu.be/vzs5VPTf4QQ?t=23m20s
          1 Reply Last reply
          1
          • GrecKoG GrecKo

            Do you have a specific reason doing all that in C++?
            It's considered bad practice.

            C Offline
            C Offline
            ChrisTof
            wrote on last edited by
            #5

            @GrecKo
            Yes, I have reasons to do all of that in C++. Even more, I have no choice.

            @raven-worx
            Thank you very much! That is what I needed. In my case, I have to refer to the window variable:

            QQmlContext* rootItemContext = QQmlEngine::contextForObject(window);
            QQuickItem *object = qobject_cast<QQuickItem*>(component.beginCreate(rootItemContext))
            
            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