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. Singal not working correct when filtering in QSortFilterProxyModel
Forum Updated to NodeBB v4.3 + New Features

Singal not working correct when filtering in QSortFilterProxyModel

Scheduled Pinned Locked Moved Solved General and Desktop
28 Posts 3 Posters 1.6k 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.
  • Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Gabber said in Singal not working correct when filtering in QSortFilterProxyModel:

    Does anyone know and can explain this to me?

    Because setFilterRegularExpression() changes the current selection

    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
    • Christian EhrlicherC Christian Ehrlicher

      @Gabber said in Singal not working correct when filtering in QSortFilterProxyModel:

      Does anyone know and can explain this to me?

      Because setFilterRegularExpression() changes the current selection

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #3

      @Christian-Ehrlicher
      Okay, thanks for the info. Do you have any idea how I can "work around" this or what I should change so this doesn't happen? I need the filtering...

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

        I don't see why this slot should not be called when a filter is applied.
        But you can e.g. disconnect the signal during the call.

        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
        • Christian EhrlicherC Christian Ehrlicher

          I don't see why this slot should not be called when a filter is applied.
          But you can e.g. disconnect the signal during the call.

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #5

          @Christian-Ehrlicher
          What I actually want to achieve: I have 3 models mFungusProxyModel, mSynonymProxyModel and mGermanNameProxyModel. mFungusProxyModel contains all data about my mushrooms, mSynonyProxyModel contains all synonyms of the mushrooms and mGermanNameProxyModel contains all German names of the mushrooms. One of the three, should be displayed in my QListView depending on which index is selected in my QComboBox. If I then press on an index/item/value, the mushroom data shall be displayed in my QLineEdits, the corresponding synonyms in a QListView and the German names also in a QLinEdit. Since the data is available in 3 different models, I have to filter them of course and as you can see I have done that using my own slots. Unfortunately, this does not work as I imagined, because it jumps back into the slot fungusItemChanged() when filtering the mFungusProxyModel.

          Can you please help me how best to implement this? Best by code examples or links where I can read things. Please don't blame me, this is my first project in programming and especially with Qt.

          Thanks for your help :)

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

            Hi,

            The question is: why are you changing the mFungusProxyModel regular expression when the synonym changes ?

            The synonyms shall match the currently selected fungus so it does not make sense to change the mFungusProxyModel when toying with the synonyms.

            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
            • SGaistS SGaist

              Hi,

              The question is: why are you changing the mFungusProxyModel regular expression when the synonym changes ?

              The synonyms shall match the currently selected fungus so it does not make sense to change the mFungusProxyModel when toying with the synonyms.

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #7

              @SGaist said in Singal not working correct when filtering in QSortFilterProxyModel:

              The question is: why are you changing the mFungusProxyModel regular expression when the synonym changes ?

              Because I have three different tables in my database that have information about a mushroom. For example, the table Mushrooms contains the id, the name, the genus, the species, the family, the order and the note. This data is then assigned to the mFungusProxyModel. The synonyms are stored in the Synonyms table, which has a 1:N relationship to the Mushroom table. Thus, the table contains the id, the synonym name and the fungusID. This data is then assigned to the mSynonymProxyModel and so on. So that I get now the data into the appropriate QLineEdit's etc., I must look for in the mFungusProxyModel.

              I hope I could answer your question. If you have a better / nicer idea how I can implement the whole thing I will gladly take the tip. I also like to share my code if that helps you.

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

                That I understood, my question is really: why selecting a synonym should trigger an update to the fungus proxy ?

                The thing is:

                • select a fungus
                • update the synonym filter

                That is logic.

                Then:

                • select a synonym
                • update the fungus filter

                Why ? As you said, you have a set of synonym that matches one fungus. So why should it have any influence on the current fungus selection ?

                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
                • SGaistS SGaist

                  That I understood, my question is really: why selecting a synonym should trigger an update to the fungus proxy ?

                  The thing is:

                  • select a fungus
                  • update the synonym filter

                  That is logic.

                  Then:

                  • select a synonym
                  • update the fungus filter

                  Why ? As you said, you have a set of synonym that matches one fungus. So why should it have any influence on the current fungus selection ?

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #9

                  @SGaist said in Singal not working correct when filtering in QSortFilterProxyModel:

                  Then:

                  select a synonym
                  update the fungus filter

                  Why ?

                  I have to query the data that belong to the synonym and are stored in the mushroom table and I do that by filtering the ID in the mFungusProxyModel. I hope I didn't miss the answer again. I am glad about your help

                  Do you have any idea how I can make it better?

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

                    That's the thing that is confusing: you already filtered the fungus model since you have the synonyms. So there's no need to filter it again. Just grab the data you want directly.

                    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
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #11

                      I'm not clear where you see the mFungusProxyModel's filtering? If I select "Synonyms" in the QComboBox and then click on one, I don't have the data from the mFungusProxyModel yet, but only the data from the mSynonymProxyModel or am I missing something here?

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

                        Wait, do you mean you may have the synonyms "unfiltered" ?

                        The logic as I understand it is:

                        • Fungus is the central model
                        • If none is selected, no synonyms shall be available
                        • Select a fungus
                        • Synonyms get populated/filtered
                        • Select a synonym
                        • Grab the data from fungus

                        Why do you need to grab the data on synonym selection rather than directly when you select a fungus ?

                        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
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by A Former User
                          #13

                          I'll try again to explain in abstract terms what I'm trying to do. I have a QListView in which names are to be displayed. It should be possible to distinguish between mushroom names, synonym names and German names. The whole thing I realize via QComboBox where I can select which model should be displayed (mFungusProxyModel, mSynonymProxyModel or mGermanNameModel). If for example in the QComboBox "Mushroom name" is selected and I choose one of thousands of mushrooms, all related data should be displayed in my QLineEdits etc. (see screenshot). If I select "Synonyms" in the QComboBox and select one out of thousands, all related data should appear.

                          A small example for this: I have a mushroom with the name "ABC" which has the associated synonym "XYZ". If I select "Mushroom names" in the QComboBox and then click on the mushroom "ABC", all data should appear. If I select then e.g. in the QComboBox "Synonyms" and click on the synonym "XYZ" the same data should appear as before with the mushroom "ABC".

                          And exactly for this I use the filtering in the mFungusProxyModel in the function synonymItemChanged()

                          myProgramm.png
                          myProgramm2.png

                          Am I doing the whole thing correctly then or are there any suggestions for improvement?

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

                            Ok, clearer now.

                            Did you consider using a QDataWidgetMapper to populate your widgets ?

                            That would simplify the data population part.

                            Set it to the correct entry on your table and voila.

                            Beside that, you need to decouple things a bit. You are trying to do too many things at the same time.

                            From the looks of it:

                            • One model per possibility which are independent from each other as they have different purposes. The ones you use to populate your QListView
                            • On selection, either extract the data from the index directly to populate your widgets or use QDataWidgetMapper to point to the correct records of the main model(s)

                            You can even consider using three QListView rather than swapping your proxies.

                            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
                            • SGaistS SGaist

                              Ok, clearer now.

                              Did you consider using a QDataWidgetMapper to populate your widgets ?

                              That would simplify the data population part.

                              Set it to the correct entry on your table and voila.

                              Beside that, you need to decouple things a bit. You are trying to do too many things at the same time.

                              From the looks of it:

                              • One model per possibility which are independent from each other as they have different purposes. The ones you use to populate your QListView
                              • On selection, either extract the data from the index directly to populate your widgets or use QDataWidgetMapper to point to the correct records of the main model(s)

                              You can even consider using three QListView rather than swapping your proxies.

                              ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #15

                              @SGaist said in Singal not working correct when filtering in QSortFilterProxyModel:

                              Did you consider using a QDataWidgetMapper to populate your widgets ?

                              Yes, I use QDataWidgetMapper to populate my widgets

                              @SGaist said in Singal not working correct when filtering in QSortFilterProxyModel:

                              Beside that, you need to decouple things a bit. You are trying to do too many things at the same time.

                              What exactly do you mean by that? Can you explain that a bit more precisely and especially at what point?

                              @SGaist said in Singal not working correct when filtering in QSortFilterProxyModel:

                              One model per possibility which are independent from each other as they have different purposes. The ones you use to populate your QListView

                              I have that. mFungusProxyModel is for the table fungus, mSynonymProxyModel is for the table synonyms and mGermanNameProxyModel is for german name table.

                              @SGaist said in Singal not working correct when filtering in QSortFilterProxyModel:

                              On selection, either extract the data from the index directly to populate your widgets or use QDataWidgetMapper to point to the correct records of the main model(s)

                              What you say makes sense, but I don't know how to filter by ID if I'm only working with the QModelIndex, for example. Can you explain this to me in more detail? Maybe also with an example?

                              @SGaist said in Singal not working correct when filtering in QSortFilterProxyModel:

                              You can even consider using tree QListView rather than swapping your proxies.

                              Do you mean a QTreeView?

                              If I then put all the models in one QTreeView, how does the selection of the correct model work?

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

                                My bad, three QListView not tree.

                                Get the source index from what was selected and then use QDataWidgetMapper::setCurrentModelIndex.

                                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
                                • SGaistS SGaist

                                  My bad, three QListView not tree.

                                  Get the source index from what was selected and then use QDataWidgetMapper::setCurrentModelIndex.

                                  ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #17

                                  @SGaist said in Singal not working correct when filtering in QSortFilterProxyModel:

                                  My bad, three QListView not tree.

                                  I almost thought that you meant three QListViews, but then I wasn't sure. The only question that comes to my mind is: How can I change the three QListViews? Since my GUI was created via the designer. How do I hide the ui->listview and how do I activate the QListView *secondList = new QListView; so that it fits into my layout?

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

                                    Put them in a QStackedWidget or QStackedLayout and switch the one to view based on your combo box selection.

                                    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
                                    • SGaistS SGaist

                                      Put them in a QStackedWidget or QStackedLayout and switch the one to view based on your combo box selection.

                                      ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #19

                                      @SGaist said in Singal not working correct when filtering in QSortFilterProxyModel:

                                      Put them in a QStackedWidget

                                      I managed to do that and it works as expected.

                                      @SGaist said in Singal not working correct when filtering in QSortFilterProxyModel:

                                      Get the source index from what was selected and then use QDataWidgetMapper::setCurrentModelIndex.

                                      I'm just not quite sure how you mean that. In my constructor I do this:

                                          secondList = new QListView;
                                          thirdList = new QListView;
                                          firstList = new QListView;
                                          firstList->setModel(mFungusProxyModel);
                                          firstList->setModelColumn(1);
                                          secondList->setModel(mSynonymProxyModel);
                                          secondList->setModelColumn(1);
                                          thirdList->setModel(mGermanNameProxyModel);
                                          thirdList->setModelColumn(0);
                                      
                                          ui->stackedWidget->addWidget(firstList);
                                          ui->stackedWidget->addWidget(secondList);
                                          ui->stackedWidget->addWidget(thirdList);
                                      
                                          connect(ui->modelSelection, QOverload<int>::of(&QComboBox::activated),
                                                      ui->stackedWidget, &QStackedWidget::setCurrentIndex);
                                          connect(firstList->selectionModel(), &QItemSelectionModel::currentChanged, this, &MainWindow::fungusItemChanged);
                                          connect(secondList->selectionModel(), &QItemSelectionModel::currentChanged, this, &MainWindow::synonymItemChanged);
                                      

                                      And my two slot's looks like this:

                                      void MainWindow::fungusItemChanged(const QModelIndex &index) const
                                      {
                                          const int id = mFungusProxyModel->data(index.siblingAtColumn(Column::Id)).toInt();
                                          const QString redlist = mFungusProxyModel->data(index.siblingAtColumn(Column::Assignment)).toString();
                                      
                                          if(redlist.isEmpty()){
                                              ui->redlist->setCurrentIndex(Index::None);
                                          }
                                      
                                          mSynonymFilterModel->setFilterRegularExpression(idFilter(id));
                                          mSynonymFilterModel->setFilterKeyColumn(Column::FungusId);
                                      
                                          ui->synonymList->setModel(mSynonymFilterModel);
                                          ui->synonymList->setModelColumn(Column::Name);
                                      
                                          mMapper->setCurrentModelIndex(index); //mMapper is QDataWidgetMapper
                                      
                                      }
                                      
                                      void MainWindow::synonymItemChanged(const QModelIndex &index) const
                                      {
                                          const int id = mSynonymProxyModel->data(index.siblingAtColumn(Column::FungusId)).toInt();
                                      
                                          mFungusProxyModel->setFilterRegularExpression(idFilter(id)) ;
                                          mFungusProxyModel->setFilterKeyColumn(Column::Id);
                                      
                                          mSynonymFilterModel->setFilterRegularExpression(idFilter(id));
                                          mSynonymFilterModel->setFilterKeyColumn(Column::FungusId);
                                      
                                          ui->synonymList->setModel(mSynonymFilterModel);
                                          ui->synonymList->setModelColumn(Column::Name);
                                      
                                          QModelIndex fungusIndex = mFungusProxyModel->index(0,0);
                                      
                                          const QString redlist = mFungusProxyModel->data(fungusIndex.siblingAtColumn(Column::Assignment)).toString();
                                      
                                          if(redlist.isEmpty()){
                                              ui->redlist->setCurrentIndex(Index::None);
                                          }
                                      
                                          mMapper->setCurrentModelIndex(fungusIndex); //mMapper is QDataWidgetMapper
                                      }
                                      

                                      If I take now for example the secondList (synonym list) and select a synonym I receive the QModelIndex (by the connect it calls me the slot synonymItemChanged(QModelIndex)). But now it is not clear to me, how I should get from this QModelIndex of the mSynonymProxyModel to the one record of the mFungusProxyModel via the QModelIndex. Can you explain this to me?

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

                                        Which model did you set on your mapper ?

                                        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
                                        • SGaistS SGaist

                                          Which model did you set on your mapper ?

                                          ? Offline
                                          ? Offline
                                          A Former User
                                          wrote on last edited by A Former User
                                          #21

                                          @SGaist said in Singal not working correct when filtering in QSortFilterProxyModel:

                                          Which model did you set on your mapper ?

                                          mFungusProxyModel. My mapping looks like this:

                                                  mFungusProxyModel->setSourceModel(mFungusDataModel);       
                                          
                                                  mMapper->setModel(mFungusProxyModel);
                                                  mMapper->setItemDelegate(new ProxySqlRelationalDelegate(mMapper));
                                                  mMapper->addMapping(ui->number, mFungusDataModel->fieldIndex("ID"), "text");
                                                  mMapper->addMapping(ui->fullname, mFungusDataModel->fieldIndex("Vollname"));
                                                  mMapper->addMapping(ui->genus, mFungusDataModel->fieldIndex("Gattung"));
                                                  mMapper->addMapping(ui->kind, mFungusDataModel->fieldIndex("Art"));
                                                  mMapper->addMapping(ui->family, mFungusDataModel->fieldIndex("Familie"));
                                                  mMapper->addMapping(ui->order, mFungusDataModel->fieldIndex("Ordnung"));
                                                  mMapper->addMapping(ui->notes, mFungusDataModel->fieldIndex("Anmerkung"));
                                                  mMapper->addMapping(ui->redlist, mFungusDataModel->fieldIndex("Zuordnung"));
                                          
                                          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