Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QQMLComponent not showing up in main Window
Forum Updated to NodeBB v4.3 + New Features

QQMLComponent not showing up in main Window

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 912 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.
  • A Offline
    A Offline
    abanksdev
    wrote on last edited by
    #1

    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
    0
    • A Offline
      A Offline
      abanksdev
      wrote on last edited by
      #2

      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
      0
      • K Offline
        K Offline
        Kishore Ravikumar
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          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
          0

          • Login

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