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
Forum Updated to NodeBB v4.3 + New Features

displaying only part of model

Scheduled Pinned Locked Moved Solved Brainstorm
6 Posts 3 Posters 752 Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on 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...

    JonBJ 1 Reply Last reply
    0
    • mzimmersM mzimmers

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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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.

      mzimmersM 1 Reply Last reply
      3
      • JonBJ JonB

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

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on 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.

        mzimmersM 1 Reply Last reply
        0
        • mzimmersM mzimmers has marked this topic as solved on
        • mzimmersM mzimmers

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

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on 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.

          SGaistS 1 Reply Last reply
          2
          • mzimmersM mzimmers

            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.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by SGaist
            #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

            mzimmersM 1 Reply Last reply
            1
            • SGaistS SGaist

              @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;
                  }
              };
              
              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on 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

              • Login

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