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. Is it possible to filter model by value in Primary Key column?
Forum Updated to NodeBB v4.3 + New Features

Is it possible to filter model by value in Primary Key column?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 2 Posters 2.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.
  • S Offline
    S Offline
    SergeyK12
    wrote on last edited by
    #1

    Hi everybody!
    Could somebody help me to find out the way of filter model.
    I have the following model:

    id (PK) / column2 / column3 ....

    i know (id) value and i need to find a row with this id in model.
    It's possible to sort by QString value, but there is no standart way to filter by int.
    How could this problem be done?

    I'm trying to use QSortProxyModel with

    proxy->setFilterRegExp(QRegExp(QString::number(k)));
    proxy->setFilterKeyColumn(DataBase::U_ID);

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

      Hi and welcome to devnet,

      I don't think it is out of the box however you can take inspiration on the Custom Filter Model example and re-implement the filterAcceptsRow method.

      Hope it helps.

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

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        I don't think it is out of the box however you can take inspiration on the Custom Filter Model example and re-implement the filterAcceptsRow method.

        Hope it helps.

        S Offline
        S Offline
        SergeyK12
        wrote on last edited by SergeyK12
        #3

        @SGaist Hi. Thanks for your reply. I saw this example several times today and still can't understand how to deal with
        filterAcceptsColumn/ filterAcceptsRow function. There is two parameters,

        int source, const QModelIndex &source_parent

        and what does they mean? For example source - is my serching id integer, but what is the second one? When i call this reimplemented method from MySortProxyModel i have to give this parameter into function.

        And as i know this far, methods i calling from proxy model must change the instance of proxy model.
        Should i,

        for(int i=0; i<sourceModel()->rowCount(); i++)
        {
        int id_tmp = sourceModel()->data(sourceModel()->index(i,0)).toInt();
        if(source == id_tmp)
        {
        //do something to change model instance
        }
        }

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SergeyK12
          wrote on last edited by
          #4

          By reading documentation more and more, i mention that this methods

          filterAcceptsColumn()
          filterAcceptsRow()

          used for filter current row or column.

          But, why there is no method like setFilterRegExp for other standart classes (int, bool for example)
          I just need to get a single row in my proxy model which contain in its Primary Key id column the value i specifyed.
          (It's easy with QString but difficult with int WTF)

          I't so dissapointing to reimplement everything in Qt for every simple task. And, yeah, i don't anderstand the way it working.

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

            The first parameter is the source model row you are going to inspect. The second is the parent item, usually only avoid when implementing a tree model.

            No, you shouldn't modify anything in the source model from that function. Just return whether the row passed as parameter should be returned for the filtering you want to do.

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

            S 1 Reply Last reply
            0
            • SGaistS SGaist

              The first parameter is the source model row you are going to inspect. The second is the parent item, usually only avoid when implementing a tree model.

              No, you shouldn't modify anything in the source model from that function. Just return whether the row passed as parameter should be returned for the filtering you want to do.

              S Offline
              S Offline
              SergeyK12
              wrote on last edited by
              #6

              @SGaist
              But how should i specify id i searching for?
              What if i didn't try to implement tree model. I only need to obtain a QSortProxyModel with a singl row (in source model more than 100 rows), which i put in QTableView. (with transpose row and columns)

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

                Like in the example, you add your own setter function for that.

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

                S 1 Reply Last reply
                0
                • SGaistS SGaist

                  Like in the example, you add your own setter function for that.

                  S Offline
                  S Offline
                  SergeyK12
                  wrote on last edited by
                  #8

                  @SGaist
                  I got it. I dont need to call this method by myself . QTableView will call it.
                  And i only need to add new private varible with its setter wich i (variable) allow to use in filterAcceptsColumn().
                  And when this method return true, QTableView will show me the instance ?
                  And thats mean i shoul go along this row or column, and compare cell value with my specifyed id.
                  ANd more, i dont eed to do any cicle by myself because QTableView will call this method in a cycle and give me const QModelIndex &source_parent which value i could compare.

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

                    Yes, that's that.

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

                    S 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Yes, that's that.

                      S Offline
                      S Offline
                      SergeyK12
                      wrote on last edited by
                      #10

                      @SGaist
                      It is worked well. Finally, after a day, it makes sense.

                      Thank you very much! I do not know that I have done without your help!

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

                        You're welcome !

                        Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

                        Note, you may have to first click on "Ask as question" before you can mark it solved.

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

                        S 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          You're welcome !

                          Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

                          Note, you may have to first click on "Ask as question" before you can mark it solved.

                          S Offline
                          S Offline
                          SergeyK12
                          wrote on last edited by
                          #12

                          @SGaist
                          Done

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

                            Thanks !

                            Happy coding :)

                            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

                            • Login

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