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 29 Jan 2015, 06:51 last edited by
    #1

    Hello
    I have QSortFilterProxyModel which is set to QListView, I have line edit to search in list when I search item and I select it and than press to add new item(without clearing the filter line) the selection disappears. how can I keep it? and one more thing here when i select item and then do a search for another the result gets selected!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 29 Jan 2015, 07:30 last edited by
      #2

      the question now is how exactly do you add the new item? Which methods to you use? Are you calling reset() after adding?

      --- 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 29 Jan 2015, 07:38 last edited by
        #3

        I do not call reset, I call
        @
        proxy->setSourceModel(model)
        @
        again, without it how sourceModel will be updated?

        1 Reply Last reply
        0
        • R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 29 Jan 2015, 07:41 last edited by
          #4

          you wrote
          [quote author="tokafr" date="1422514280"]... and than press to add new item...[/quote]
          I wanted to know how do you add the new items? Or do i misunderstand something here?

          Anyways you should post some code to clear things up.

          --- 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 29 Jan 2015, 07:46 last edited by
            #5

            I have a button that shows me a dialog with fields, I fill them and press add, so item is added. the code of it is too large to write here :)

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tokafr
              wrote on 29 Jan 2015, 08:07 last edited by
              #6

              @ QModelIndexList indexes = listWidget->selectionModel()->selectedIndexes();
              QString text;
              QModelIndex ind;
              foreach(QModelIndex index, indexes)
              {
              //text = index.data(Qt::UserRole).toString();
              ind = proxy->mapToSource(index);
              }
              model->addRow(newConnection);
              proxy->setSourceModel(model);
              emit searchLine->textChanged(searchLine->text());
              ind = proxy->mapFromSource(ind);
              listWidget->selectionModel()->select(ind,QItemSelectionModel::Select);@

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tokafr
                wrote on 29 Jan 2015, 08:09 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
                • R Offline
                  R Offline
                  raven-worx
                  Moderators
                  wrote on 29 Jan 2015, 08:14 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 29 Jan 2015, 08:16 last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      raven-worx
                      Moderators
                      wrote on 29 Jan 2015, 08:27 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 29 Jan 2015, 09:56 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
                        • R Offline
                          R Offline
                          raven-worx
                          Moderators
                          wrote on 29 Jan 2015, 10:10 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 29 Jan 2015, 10:14 last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              raven-worx
                              Moderators
                              wrote on 29 Jan 2015, 10:16 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 29 Jan 2015, 10:54 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 29 Jan 2015, 11:13 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

                                  1/16

                                  29 Jan 2015, 06:51

                                  • Login

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