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. Adding QML element from C++
QtWS25 Last Chance

Adding QML element from C++

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 3.2k 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.
  • E Offline
    E Offline
    emppu
    wrote on last edited by JKSH
    #1

    Hello, I am trying to add QML elements in C++ but it doesn't work.

    I have tried to (1) find a parent, (2) create QQmlComponent, (3) create an object, and (4) setParent.

    1. QObject *parent = engine.rootObjects().first()->findChild<QObject*>("row1");;
    QQmlEngine *engine2 = new QQmlEngine;
    QQmlComponent component(engine, QUrl("qrc:/Abc.qml"), QQmlComponent::PreferSynchronous);
    
    1. QObject *rect = component.create();
    2. rect->setParent(parent);

    I added objectName: "parent" to a QML element and Abc.qml is simple:

    Rectangle {
    	width: 100
    	height: 62
    	color: green
    }
    

    What is wrong?

    JKSHJ 1 Reply Last reply
    0
    • E emppu

      Hello, I am trying to add QML elements in C++ but it doesn't work.

      I have tried to (1) find a parent, (2) create QQmlComponent, (3) create an object, and (4) setParent.

      1. QObject *parent = engine.rootObjects().first()->findChild<QObject*>("row1");;
      QQmlEngine *engine2 = new QQmlEngine;
      QQmlComponent component(engine, QUrl("qrc:/Abc.qml"), QQmlComponent::PreferSynchronous);
      
      1. QObject *rect = component.create();
      2. rect->setParent(parent);

      I added objectName: "parent" to a QML element and Abc.qml is simple:

      Rectangle {
      	width: 100
      	height: 62
      	color: green
      }
      

      What is wrong?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      Hi @emppu,

      First, you need to understand the difference between object parent and visual parent (see http://doc.qt.io/qt-5/qtquick-visualcanvas-visualparent.html ).

      Qt Quick components are shown based on their visual parent.

      1. QObject *rect = component.create();
      2. rect->setParent(parent);

      This sets the object parent, not the visual parent.

      Instead, you need to use QQuickItem::setVisualParent() QQuickItem::setParentItem(). [EDIT: Typo]

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      E 1 Reply Last reply
      1
      • JKSHJ JKSH

        Hi @emppu,

        First, you need to understand the difference between object parent and visual parent (see http://doc.qt.io/qt-5/qtquick-visualcanvas-visualparent.html ).

        Qt Quick components are shown based on their visual parent.

        1. QObject *rect = component.create();
        2. rect->setParent(parent);

        This sets the object parent, not the visual parent.

        Instead, you need to use QQuickItem::setVisualParent() QQuickItem::setParentItem(). [EDIT: Typo]

        E Offline
        E Offline
        emppu
        wrote on last edited by
        #3

        @JKSH Thanks!
        In Qt 5.4, the following sets the visual parent.
        child->setParentItem((QQuickItem*)parent);

        1 Reply Last reply
        0
        • E Offline
          E Offline
          emppu
          wrote on last edited by
          #4

          The follow-up question:
          Is it possbie to specify the index of insertion? Or change the existing order of children?

          JKSHJ 1 Reply Last reply
          0
          • E emppu

            The follow-up question:
            Is it possbie to specify the index of insertion? Or change the existing order of children?

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            You're welcome!

            @emppu said:

            Is it possbie to specify the index of insertion? Or change the existing order of children?

            Yes. Set the "z" property (similar to "x" and "y").

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            1

            • Login

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