Evaluate QQuickItem's width and height



  • I create a QML component from C++:

    QQmlComponent *component = new QQmlComponent(m_engine,
                                    QUrl(QStringLiteral("qrc:/my.qml")));
     QQuickItem *my = qobject_cast<QQuickItem*>(component->create());
      delete component;
      my->setParent(parent);
      my->setParentItem(parent);
      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?


  • Moderators

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.