Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt's common patterns for collection change signals?
Forum Updated to NodeBB v4.3 + New Features

Qt's common patterns for collection change signals?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 918 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #3

    Hi,

    Not all Qt classes derives from QObject.

    Containers are such classes.

    It seems you are looking for the model classes.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Igor Baidiuk
      wrote on last edited by Igor Baidiuk
      #4

      In general, I want to design tree-like hierarchy of objects:

      class Node: public QObject {
          ...
          // Root type of type hierarchy
      };
      
      class Assembly: public Node {
          ...
          Q_PROPERTY(QVector<Node*> children ...)
          ...
      };
      
      class Face: public Node {
          ...
      };
      
      class Edge: public Node {
          ...
      };
      
      class Vertex: public Node {
          ...
      };
      
      class Part: public Node {
          ...
          Q_PROPERTY(QVector<Face*> faces ...)
          Q_PROPERTY(QVector<Edge*> edges ...)
          Q_PROPERTY(QVector<Vertex*> vertices ...)
          ...
      };
      

      in such a way that each object would emit signals about changes in its child sets (not just "children changed", being more specific).
      And I would prefer to not copypaste all the code each time I need another collection, since hierarchy would be more complex than one presented above.

      @JonB

      I mean such case as:

      class SomeClass: public QObject {
          Q_OBJECT
          Q_PROPERTY(QVector<SomeValueType> collection READ collection WRITE setCollection NOTIFY collectionChanged)
      public:
          QVector<SomeValueType> collection() const;
          void setCollection(QVector<SomeValueType>);
          // some helper methods
          void collectionAddElement(SomeValueType);
          void collectionRemoveElement(int);
      
      signals:
          void collectionChanged(int start, int oldCount, int newCount);
      };
      

      Versus approach with separate collection wrapper class

      class CollectionSignals : public QObject {
          Q_OBJECT
      signals:
          changed(int start, int oldCount, int newCount);
      };
      
      template<typename T>
      class Collection: public CollectionSignals {
      public:
          int size() const;
          void add(T value);
          T remove(int at);
          T get(int at);
      
          QVector<T> asVector() const;
          // ... other methods
      };
      
      class SomeClass: public QObject {
          Q_OBJECT
          Q_PROPERTY(Collection<SomeValueType>* collection READ collection)
      public:
          Collection<SomeValueType>* collection() const { return &_collection; }
      
      private:
          Collection<SomeValueType> _collection;
      };
      

      First approach seems more lined up with general Qt ideology, although it requires more manual coding.
      Second approach removes need for most boilerplate, and gives common functionality for all similar collections, although it makes each such collection into separate QObject (with its overhead) and doesn't seem to fit that well with Qt's intended structuring. I.e. access to list of its values from QML would be someObject.collection.asVector(), no simple property binding via elements: someobject.collection etc.

      @SGaist

      Sorry, I may've misread you. QVector, QList, even QQmlListProperty don't provide signals for specifically their changes. And QAbstractItemModel is IMO overkill since it represents recursively nested rectangular tables.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #5

        Use a QAbstractItemModel - it does all what you describe above.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • I Offline
          I Offline
          Igor Baidiuk
          wrote on last edited by
          #6

          @Christian-Ehrlicher As I mentioned above, QAbstractItemModel is too abstract. It requires considerable amount of coding for implementation, and is rather cumbersome as simple collection of values. I'm looking for something that is usable from both UI and C++ logic sides.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #7

            You might find it overkill but in fact, it provides all what you require including UI and backend usability.

            You should also take into account that QObject based classes cannot be copied, therefore you would have to work only with containers of pointers. Also, QObject, while not being a superheavy class would also adds up pretty quickly to the memory cost of your containers.

            Keeping your data structure as standard containers and putting a model on top of it will keeps things simple and clean. You can add API's to the model in order to manipulate the underlying data structure and in theses methods you'll be able to properly trigger the various signals you need for your UI to be updated automatically.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • I Offline
              I Offline
              Igor Baidiuk
              wrote on last edited by
              #8

              @SGaist

              Am I right you're suggesting something like this:

              class MyItemModel: QAbstractItemModel {
                  // ...
                  // implement iterators, simple item getters etc.
              };
              
              class SomeClass: public QObject {
                  Q_OBJECT
                  Q_PROPERTY(MyItemModel* collection ...)
              };
              
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #9

                Your MyItemModel class is not defined enough to be able to give you an answer.

                However, like I wrote before, the model sits on top of you data structure, it does not replace it.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  Igor Baidiuk
                  wrote on last edited by
                  #10

                  I know this. What I'm asking is what's the recommended way to implement that underlying collection with change notifications in mind.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    The collection does not need to have a change notification, the model sends the notifications.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      Igor Baidiuk
                      wrote on last edited by
                      #12

                      @Christian-Ehrlicher So are you suggesting

                      class SomeClass: public QObject {
                          ...
                          Q_PROPERTY(QAbstractItemModel* collection ...)
                          ...
                      };
                      

                      ?

                      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