Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. displaying only part of model

displaying only part of model

Scheduled Pinned Locked Moved Solved Brainstorm
6 Posts 3 Posters 721 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
    mzimmers
    wrote on 27 May 2023, 01:45 last edited by
    #1

    Hi all -

    I have a C++ model that is exposed to QML. I access the model in what I believe is in typical fashion:

    GridView {
        model: zoneModel
        delegate: ZoneTile {
            titleText: model.name
            ...
    

    There are places in my application where I'd like to exclude the first element in the list that the model returns.

    First question: would it be preferable to accomplish this in the class that supports the model (say with a special function that slices the list), or in the QML? My sense is that the latter is preferable, but I can't figure out how to do it.

    Making it invisible isn't enough, because it still takes up space. I could set its height and width to 0, and null out all its contents, but that seems kind of like a hack. Is there a better way to do this?

    Thanks...

    J 1 Reply Last reply 27 May 2023, 06:51
    0
    • M mzimmers
      27 May 2023, 01:45

      Hi all -

      I have a C++ model that is exposed to QML. I access the model in what I believe is in typical fashion:

      GridView {
          model: zoneModel
          delegate: ZoneTile {
              titleText: model.name
              ...
      

      There are places in my application where I'd like to exclude the first element in the list that the model returns.

      First question: would it be preferable to accomplish this in the class that supports the model (say with a special function that slices the list), or in the QML? My sense is that the latter is preferable, but I can't figure out how to do it.

      Making it invisible isn't enough, because it still takes up space. I could set its height and width to 0, and null out all its contents, but that seems kind of like a hack. Is there a better way to do this?

      Thanks...

      J Offline
      J Offline
      JonB
      wrote on 27 May 2023, 06:51 last edited by
      #2

      @mzimmers
      I know nothing about QML. But if you have a source model and you want to alter it by, say, omitting the first row (element in list) it is trivially easy to do that by interposing a QAbstractProxyModel between the source model and your QML/view. Subclass QIdentityProxyModel (which passes through its source model unaltered) and have it (a) return base.rowCount() - 1 for its rowCount() override and (b) override mapToSource()/mapFromSource() to add/subtract 1 to/from the row number.

      M 1 Reply Last reply 29 May 2023, 17:50
      3
      • J JonB
        27 May 2023, 06:51

        @mzimmers
        I know nothing about QML. But if you have a source model and you want to alter it by, say, omitting the first row (element in list) it is trivially easy to do that by interposing a QAbstractProxyModel between the source model and your QML/view. Subclass QIdentityProxyModel (which passes through its source model unaltered) and have it (a) return base.rowCount() - 1 for its rowCount() override and (b) override mapToSource()/mapFromSource() to add/subtract 1 to/from the row number.

        M Offline
        M Offline
        mzimmers
        wrote on 29 May 2023, 17:50 last edited by
        #3

        @JonB hey Jon - thanks for the suggestion. I've never really used proxy models before, but your suggestion prompted me to read a little more about them, and I can definitely see how they could also be useful in this situation.

        I also decided, however, that what I'm trying to do is dumb, so I'm going to take a different approach. Basically I need to use the zones in the list as a filter for related items. I was trying to display them in a tab bar, but that's looking like a lousy idea, so I'm going to find a different way of displaying them.

        Thanks for the suggestion, though; I'll be using it in the future.

        M 1 Reply Last reply 5 Jun 2023, 22:30
        0
        • M mzimmers has marked this topic as solved on 29 May 2023, 17:50
        • M mzimmers
          29 May 2023, 17:50

          @JonB hey Jon - thanks for the suggestion. I've never really used proxy models before, but your suggestion prompted me to read a little more about them, and I can definitely see how they could also be useful in this situation.

          I also decided, however, that what I'm trying to do is dumb, so I'm going to take a different approach. Basically I need to use the zones in the list as a filter for related items. I was trying to display them in a tab bar, but that's looking like a lousy idea, so I'm going to find a different way of displaying them.

          Thanks for the suggestion, though; I'll be using it in the future.

          M Offline
          M Offline
          mzimmers
          wrote on 5 Jun 2023, 22:30 last edited by
          #4

          And...the future is here!

          class ZoneProxyModel : public QSortFilterProxyModel {
              Q_OBJECT
          public:
              explicit ZoneProxyModel(QObject *parent = nullptr);
          protected:
              bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override {
                  bool rc = true;
                  if (sourceRow == 0)
                      rc = false;
                  return rc;
              }
          };
          

          Works perfectly. Thanks again, Jon.

          S 1 Reply Last reply 6 Jun 2023, 19:41
          2
          • M mzimmers
            5 Jun 2023, 22:30

            And...the future is here!

            class ZoneProxyModel : public QSortFilterProxyModel {
                Q_OBJECT
            public:
                explicit ZoneProxyModel(QObject *parent = nullptr);
            protected:
                bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override {
                    bool rc = true;
                    if (sourceRow == 0)
                        rc = false;
                    return rc;
                }
            };
            

            Works perfectly. Thanks again, Jon.

            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 6 Jun 2023, 19:41 last edited by SGaist 6 Jun 2023, 19:42
            #5

            @mzimmers hi,

            You could even make it simpler:

            class ZoneProxyModel : public QSortFilterProxyModel {
                Q_OBJECT
            public:
                explicit ZoneProxyModel(QObject *parent = nullptr);
            protected:
                bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override {
                    Q_UNUSED(sourceParent);
                    return sourceRow != 0;
                }
            };
            

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

            M 1 Reply Last reply 6 Jun 2023, 20:53
            1
            • S SGaist
              6 Jun 2023, 19:41

              @mzimmers hi,

              You could even make it simpler:

              class ZoneProxyModel : public QSortFilterProxyModel {
                  Q_OBJECT
              public:
                  explicit ZoneProxyModel(QObject *parent = nullptr);
              protected:
                  bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override {
                      Q_UNUSED(sourceParent);
                      return sourceRow != 0;
                  }
              };
              
              M Offline
              M Offline
              mzimmers
              wrote on 6 Jun 2023, 20:53 last edited by
              #6

              @SGaist maybe it's because I'm old school, or maybe it's because I have enough trouble reading other people's code, but...whatever the reason, I prefer to explicitly derive a function's return value, assign it to a variable, and return that variable. I realize it accomplishes nothing, and in the absence of a good optimizing compiler, is actually fractionally slower, but...it's just how I roll.

              1 Reply Last reply
              0

              3/6

              29 May 2023, 17:50

              • Login

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