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. QML + QList<QObject*> won't work with setContextProperty
Qt 6.11 is out! See what's new in the release blog

QML + QList<QObject*> won't work with setContextProperty

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 433 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
    Malachi
    wrote on last edited by Malachi
    #1

    I'm attempting to do something akin to https://doc.qt.io/qt-6/qtquick-modelviewsdata-cppmodels.html . Specifically I want a QList of QObject-derived instances to render in a QML ListView.

    If I follow the examples exactly, it works. However, if I attempt to acquire the model through a custom Session object set to context, I run into issues.

    Specifically, the session code looks like:

    class Session : public QObject
    {
        Q_OBJECT
    
        typedef QVariant result_type;
    
        Q_PROPERTY(result_type items READ items NOTIFY itemsChanged)
    ...
    };
    

    Then effectively we do a:

    ListView {
        model: session.items
    }
    

    and in main:

        Session session;
    
        context->setContextProperty("session", &session);
    
    

    I've spent around 4 hours on it now. Everything yields a "Required property was not initialized" despite verifying there is content in session.items

    I'm happy to expand the example or feel free to browse full proof of concept code here https://bitbucket.org/malachib/playground.qt/src/master/src/PGQT-43/

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Malachi
      wrote on last edited by Malachi
      #2

      I have observed that if the consuming QML uses modelData rather than properties, things work as expected.

      i.e.

      delegate: RowLayout {
          required property string name
          Text { Layout.fillWidth: true; text: parent.name }
      }
      

      exhibits the problem at hand, while

      delegate: RowLayout {
          Text { Layout.fillWidth: true; text: modelData.name }
      }
      

      Works.

      According to the documentation, we expect the property version to work as well: "The QObject* is available as the modelData property. As a convenience, the properties of the object are also made available directly in the delegate's context"

      So, the problem is still unsolved.

      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