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 search using QSortFilterProxyModel
Qt 6.11 is out! See what's new in the release blog

how to search using QSortFilterProxyModel

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.3k 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.
  • L LT-K101

    I'm trying to perform search a in QTableWidget using QSortFilterProxyModel() but I'm getting an error message saying self.ui.tableWidget.setModel(filter_proxy_model)
    TypeError: QTableWidget.setModel() is a private method
    . Please I need assistance, below is the code I used. Thanks in advance.

       companies = ('Apple', 'Facebook', 'Google', 'Amazon', 'Walmart', 'Dropbox', 'Starbucks', 'eBay', 'Canon')
            model = QStandardItemModel(len(companies), 1)
            model.setHorizontalHeaderLabels(['Company'])
    
            for row, company in enumerate(companies):
                item = QStandardItem(company)
                model.setItem(row, 0, item)
    
            filter_proxy_model = QSortFilterProxyModel()
            filter_proxy_model.setSourceModel(model)
            filter_proxy_model.setFilterCaseSensitivity(Qt.CaseInsensitive)
            filter_proxy_model.setFilterKeyColumn(0)
    
    
            self.ui.lineEdit.textChanged.connect(filter_proxy_model.setFilterRegExp)
    
          
            self.ui.tableWidget.verticalHeader().setSectionResizeMode(QHeaderView.Stretch)
            self.ui.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
            self.ui.tableWidget.setModel(filter_proxy_model)
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #2

    @LT-K101
    Although I cannot actually see it documented, the error message QTableWidget.setModel() is a private method tells you that you cannot call setModel() directly on a QTableWidget. You would have to subclass it to avoid this message.

    However, I don't think QTableWidget supports you changing its model. It has an internal model which it wishes to use. If you want to interpose a QSortFilterProxyModel you probably need to move to a QTableView.

    See also https://stackoverflow.com/questions/1137732/setting-the-model-to-a-qtablewidget

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

      Hi,

      As @JonB rightly pointed, QTableWidget is a convenience widget that is provided with model included as well as dedicated item class.

      You are anyway creating your own model for your companies so using QTableWidget would not make sense in any case. So use QTableView. In the absolute, you have a single column model so it would even make more sense to use a QListView and since it's a string list, the QStringListModel.

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

      L 1 Reply Last reply
      3
      • L Offline
        L Offline
        LT-K101
        wrote on last edited by
        #4

        @JonB Thanks QTableView solved the error.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          As @JonB rightly pointed, QTableWidget is a convenience widget that is provided with model included as well as dedicated item class.

          You are anyway creating your own model for your companies so using QTableWidget would not make sense in any case. So use QTableView. In the absolute, you have a single column model so it would even make more sense to use a QListView and since it's a string list, the QStringListModel.

          L Offline
          L Offline
          LT-K101
          wrote on last edited by
          #5

          @SGaist Thanks a lot for your response. What I want to actually implement is, I want to use say two columns in my database table as search parameters namely First Name and Last Name.So when the user press a letter in a search field(lineEdit) the search will return all the words having the letter pressed to be displayed in a QTableView. Can I use the approach above to accomplished this?Any advice. Thanks

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

            You are mentioning database, are you using a SQL database ?

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

            L 1 Reply Last reply
            0
            • SGaistS SGaist

              You are mentioning database, are you using a SQL database ?

              L Offline
              L Offline
              LT-K101
              wrote on last edited by
              #7

              @SGaist You right Sqlite database

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

                So the next question is whether you want the database to do the filtering or the proxy model.

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

                L 1 Reply Last reply
                1
                • SGaistS SGaist

                  So the next question is whether you want the database to do the filtering or the proxy model.

                  L Offline
                  L Offline
                  LT-K101
                  wrote on last edited by
                  #9

                  @SGaist Yes you right , so I wanted to use the key press event with an sql LIKE statement. Do you think that will be feasible?

                  JonBJ 1 Reply Last reply
                  0
                  • L LT-K101

                    @SGaist Yes you right , so I wanted to use the key press event with an sql LIKE statement. Do you think that will be feasible?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #10

                    @LT-K101
                    Yes it would be "feasible".

                    However, using key press event (or text changed signal on a line edit) sounds like you would want to append to the LIKE and re-issue a SQL query for each key. That is "expensive"! Might be OK on, say, a local SQLite database, not so keen on, say, a remote MySQL database. Or, "accumulate" key presses/changed signals over a small period, like till 1 second has passed since the last character added, and only re-issue the query then.

                    Of course if the data rows are small you can afford to read data in and use Qt client-side filtering instead.

                    You could also use a QCompleter, depending on your data size and speed.

                    L 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @LT-K101
                      Yes it would be "feasible".

                      However, using key press event (or text changed signal on a line edit) sounds like you would want to append to the LIKE and re-issue a SQL query for each key. That is "expensive"! Might be OK on, say, a local SQLite database, not so keen on, say, a remote MySQL database. Or, "accumulate" key presses/changed signals over a small period, like till 1 second has passed since the last character added, and only re-issue the query then.

                      Of course if the data rows are small you can afford to read data in and use Qt client-side filtering instead.

                      You could also use a QCompleter, depending on your data size and speed.

                      L Offline
                      L Offline
                      LT-K101
                      wrote on last edited by
                      #11

                      @JonB Thanks I used textChanged[str] signal and it worked!

                      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