Important: Please read the Qt Code of Conduct -

Evaluate QQuickItem's width and height

  • I create a QML component from C++:

    QQmlComponent *component = new QQmlComponent(m_engine,
     QQuickItem *my = qobject_cast<QQuickItem*>(component->create());
      delete component;
      QQmlEngine::setObjectOwnership(ball, QQmlEngine::CppOwnership);

    If I check my's width and heigh imediatelly after above code is executed (I print them with qDebug()), I get zeros, even width and height is set relatively to parent:

    // my.qml
    Item {
        width: parent.width
        height: parent.height

    How to force QML engine to evaluate width and height or where can one be sure width and heigh has been evaluated?

  • Hi!
    This should work:

    • Add a "ready()" signal to the root item of your qml file
    • In that qml file, add component.onCompleted: ready()
    • In C++ add some slot that shall be called once your QtQuick item is completed and connect said signal to this slot.

    Another, simpler way: In C++, after you load the qml, loop until the item says it's complete (using bool QQuickItem::isComponentComplete() const).

Log in to reply