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. List of custom object with Q_PROPERTYs as model?
QtWS25 Last Chance

List of custom object with Q_PROPERTYs as model?

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

    Hi,

    I have a vector of custom objects

     vector<MyObject> myVector;
    

    which expose their variables via Q_PROPERTY to qml:

    class MyObject:  public QObject {
      Q_OBJECT
    
      Q_PROPERTY(int prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed)
      Q_PROPERTY(int prop2 READ prop2 WRITE setProp2 NOTIFY prop2Changed)
     ...
    

    I would like to make a model out of this vector, which I can use in a repeater. I know that it is possible to use

     _eng->rootContext()->setContextProperty("myModel",QVariant::fromValue(myVector));
    

    but according to the docu

    Note: There is no way for the view to know that the contents of a QList has changed. If the QList changes, it is necessary to reset the model by calling QQmlContext::setContextProperty() again.

    I would like to avoid always calling setContextProperty. Is it possible to make a model out of my vector which automatically updates the GUI?

    Or do I understand the doc wrong: If I don't change the elements of the vector but the properties inside an element, would there be an update?

    raven-worxR 1 Reply Last reply
    0
    • M maxwell31

      Hi,

      I have a vector of custom objects

       vector<MyObject> myVector;
      

      which expose their variables via Q_PROPERTY to qml:

      class MyObject:  public QObject {
        Q_OBJECT
      
        Q_PROPERTY(int prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed)
        Q_PROPERTY(int prop2 READ prop2 WRITE setProp2 NOTIFY prop2Changed)
       ...
      

      I would like to make a model out of this vector, which I can use in a repeater. I know that it is possible to use

       _eng->rootContext()->setContextProperty("myModel",QVariant::fromValue(myVector));
      

      but according to the docu

      Note: There is no way for the view to know that the contents of a QList has changed. If the QList changes, it is necessary to reset the model by calling QQmlContext::setContextProperty() again.

      I would like to avoid always calling setContextProperty. Is it possible to make a model out of my vector which automatically updates the GUI?

      Or do I understand the doc wrong: If I don't change the elements of the vector but the properties inside an element, would there be an update?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @maxwell31
      see QQmlListProperty

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved