Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved QQMLComponent not showing up in main Window

    General and Desktop
    3
    4
    512
    Loading More Posts
    • 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
      abanksdev last edited by

      Hi there, I am following the documentation to call a QML component from c++ and display it in the main window: http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html

      In main.cpp i have the following:

      QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
          QQmlComponent component(&engine,
                  QUrl::fromLocalFile(":/Circle2.qml"));
          QObject *object = component.create();
      

      The path ":/Circle2.qml" as opposed to "Circle2.qml" or "qrc:/Circle2.qml" is the only path that does not generate the error "QQmlComponent: Component is not ready" as described in this post: https://forum.qt.io/topic/54517/solved-qqmlcomponent-component-is-not-ready/9

      HOWEVER, even without the error, the Circle component does not show up in my main window.

      my main.qml:

      import QtQuick 2.9
      import QtQuick.Window 2.2
      
      Window {
          visible: true
          width: 640
          height: 300
          title: qsTr("Hello World")
          color: "green"
      
      
          /*Circle{
              //width: parent.width/16
              //height: parent.height/8
              anchors.centerIn: parent
          }*/
      
      }
      

      and my Circle2.qml file:

      import QtQuick 2.4
      
      Rectangle{
              width: 50
              height: 50
               color: "darkblue"
               //border.color: "blue"
               border.width: 0
               radius: width*0.5
      
      }
      

      Does anyone know why my circle isnt displaying anywhere?

      1 Reply Last reply Reply Quote 0
      • A
        abanksdev last edited by

        This is all I see when i run it:

        0_1537307208470_Screen Shot 2018-09-18 at 5.46.37 PM.png

        1 Reply Last reply Reply Quote 0
        • K
          Kishore Ravikumar last edited by

          Hi, did you get the answer for it. Did you resolve it?

          1 Reply Last reply Reply Quote 0
          • jeremy_k
            jeremy_k last edited by

            The C++ code snippet in OP is missing a call to set the parent item of the instantiated component. Without that, it's not part of a scene graph.

            From https://doc.qt.io/qt-6/qqmlcomponent.html

            QQmlComponent component(engine, QUrl::fromLocalFile("MyItem.qml"));
            QQuickItem *childItem = qobject_cast<QQuickItem*>(component.create());
            childItem->setParentItem(this);
            

            Asking a question about code? http://eel.is/iso-c++/testcase/

            1 Reply Last reply Reply Quote 0
            • First post
              Last post