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. How to keep selection while adding new item to QSortFilterProxyModel?
Forum Updated to NodeBB v4.3 + New Features

How to keep selection while adding new item to QSortFilterProxyModel?

Scheduled Pinned Locked Moved General and Desktop
16 Posts 3 Posters 6.9k 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.
  • T Offline
    T Offline
    tokafr
    wrote on last edited by
    #7

    @
    void MainWindow::onNewFilterText(QString filter)
    {

    model->sort(0);
    proxy->setFilterRole(Qt::UserRole);
    proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
    proxy->setFilterWildcard(filter);

    }@

    textchanged() emits this

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #8

      why do you reset the model?
      @proxy->setSourceModel(model);@
      This is actually not needed because the model is the same right?

      --- 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
      0
      • T Offline
        T Offline
        tokafr
        wrote on last edited by
        #9

        I add item to a model, how does proxy knows that model changed.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #10

          Of course the model triggers a signal when a new row is added.

          What kind of source model do you use? Did you implement the addRow() method yourself? If so you need to make sure you emit the right signals then (by calling specific methods)

          -Actually you should reimplement insertRow().-

          See the docs for QAbstractItemModel::beginInsertRows() this will clear things up which parameters you need to pass to it.

          --- 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
          0
          • T Offline
            T Offline
            tokafr
            wrote on last edited by
            #11

            and how parameters are passed, by using virtual void setData()?
            if so the indexing here is very complicated
            @
            void Model::addRow(Connection &item)
            {
            QModelIndex parent = index(dataList.count());
            QModelIndex currentIndex = index (dataList.count() +1);
            int row = dataList.size();
            insertRows(row, 1, parent);
            QVariant connection;
            connection.setValue(item);
            setData(currentIndex,connection,Qt::EditRole);
            @

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #12

              This whole method is messed up. Since you are using a list view widget you will never ever have a parent index. So this will always be an invalid index.

              Forget about what i said you should reimplement insertRow().. sry for the confusion. Do something like this:
              @
              void Model::addConnection(Connection &item)
              {
              beginInsertRows( QModelIndex(), dataList.count(), dataList.count() ); //append 1 row to the end of the list
              dataList << item; //assuming dataList is a QList
              endInsertRows();
              }
              @

              So what happens is:

              you tell the model that you are about to insert new rows

              insert the new rows to your data structure (the same from which you get your data in the Model::data() hopefully)

              tell the model you are finished with inserting

              the model now tells the view/proxymodel that new rows were inserted

              the view updates and keeps it's selection / the proxy model reevaluates the filter for the newly added rows (when QSortFilterProxyModel::setDynamicSortFilter(true))

              Note: The proxymodel automatically forwards all the signals from the source model to the view automatically.

              --- 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
              0
              • T Offline
                T Offline
                tokafr
                wrote on last edited by
                #13

                thank you, one more thing don't i need to emit dataChanged() signal?

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #14

                  no, since you already signaled that you added a completely new row.
                  The dataChanged() signal is only necessary if data of an existing item/index has changed.

                  --- 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
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #15

                    The root problem is that your selection model is based on the proxy model, and not on the source model of the proxy. That means that if your proxy removes items due to filtering, the selection model only knows that the item has gone away, and thus the item has to be removed from the selection. The selection model has no concept of proxies.

                    KDE has created the "KLinkItemSelectionModel class"http://api.kde.org/4.5-api/kdelibs-apidocs/kdeui/html/classKLinkItemSelectionModel.html that tackles this issue. It allows mapping the selection through a proxy model. In your case, you could make a QItemSelectionModel instance on your source model, and use the proxy to get a mapped version of that in your actual view.

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tokafr
                      wrote on last edited by
                      #16

                      the problem is that after adding an item I sort the base model, and the selection index gets wrong.

                      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