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. Evaluate QQuickItem's width and height
Forum Updated to NodeBB v4.3 + New Features

Evaluate QQuickItem's width and height

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 960 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.
  • M Offline
    M Offline
    MartinD
    wrote on last edited by
    #1

    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?

    ? 1 Reply Last reply
    0
    • M MartinD

      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?

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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).

      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