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. set parent when creating a QML component from C++
Forum Update on Monday, May 27th 2025

set parent when creating a QML component from C++

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qml component c
2 Posts 2 Posters 2.1k 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.
  • L Offline
    L Offline
    lqsa
    wrote on 10 Jan 2016, 01:02 last edited by
    #1

    Hello,

    I've a QML component that is created from C++ code:

    QML Code:

    Rectangle {
            width: parent.width
            y: parent.height / 2
            color: "red"
        }
    

    C++ Code:

        QQmlComponent component(engine, QUrl(src));
        QObject *comp = component.create();
        QQuickItem *item = qobject_cast<QQuickItem*>(comp);
        item->setParentItem(root);
    

    When the component is created appears these errors:

    TypeError: Cannot read property 'height' of null
    TypeError: Cannot read property 'width' of null

    On creation, the parent property is null and not is valid until the item->setParentItem is executed. How can I set the parent item when creating the component? or how to avoid these errors?

    Best regards.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bogong
      wrote on 31 Aug 2019, 09:37 last edited by
      #2

      There are object cast need to be done for "root" object too

      QQmlComponent component(engine, QUrl(src));
          QObject *comp = component.create();
          QQuickItem *item = qobject_cast<QQuickItem*>(comp);
          item->setParentItem(qobject_cast<QQuickItem*>(root));
      

      This works for me in my project:

      // QtQuick Application
      int main(int Counter, char *Arguments[]) {
      
      	QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      	QGuiApplication Application(Counter, Arguments);
      
      	QQmlApplicationEngine Engine;
      	Engine.load(QUrl(Main));
      	if (Engine.rootObjects().isEmpty()) {
      		return -1;
      	}
      
      	QObject *oRootObject = dynamic_cast<QObject*>(Engine.rootObjects()[0]);
      	QObject *oBottomBlock = oRootObject->findChild<QObject*>("bottomBlock");
      
      	if (oBottomBlock) {
      
      		QQmlComponent oComponent(&Engine,QUrl(QString("qrc:/ButtonExit.qml")));
      		QObject *oButtonExit = oComponent.create();
      		QQuickItem *oItemButtonExit = qobject_cast<QQuickItem*>(oButtonExit);
      		oItemButtonExit->setParentItem(qobject_cast<QQuickItem*>(oBottomBlock));
      	}
      
      	return Application.exec();
      }
      
      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