Search code examples
pythonpysideqtableviewqstandarditemmodel

QTableview Select Item from filtered model


I have a QTableView which is using a QStandardItemModel. How can I select a specific row in the table with/without a search applied.

For example I typed 'y' in the search bar to filter the list to only display rows that contain the letter 'y'. When I click the button 'Select Emily' how can i make it select the correct row in the tableview, considering users can change the sort order?

enter image description here

import sys
from PySide import QtCore, QtGui


class Example(QtGui.QWidget):

    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
        self.resize(400,400)

        # controls
        model = QtGui.QStandardItemModel(5, 3)
        model.setHorizontalHeaderLabels(['NAME', 'AGE', 'CAREER'])

        people = [
            {'name': 'Kevin', 'age': 5, 'career': 'athlete'},
            {'name': 'Maggie', 'age': 13, 'career': 'banker'},
            {'name': 'Leslie', 'age': 32, 'career': 'banker'},
            {'name': 'Abby', 'age': 32, 'career': 'marketing'},
            {'name': 'Emily', 'age': 45, 'career': 'athlete'},
            {'name': 'David', 'age': 27, 'career': 'banker'},
            {'name': 'Johnny', 'age': 27, 'career': 'soccer'},
            {'name': 'Marie', 'age': 63, 'career': 'secretary'}
        ]
        for row, obj in enumerate(people):
            item = QtGui.QStandardItem(obj['name'])
            model.setItem(row, 0, item)

            item = QtGui.QStandardItem(str(obj['age']))
            model.setItem(row, 1, item)

            item = QtGui.QStandardItem(obj['career'])
            model.setItem(row, 2, item)

        proxy_model = QtGui.QSortFilterProxyModel()
        proxy_model.setSourceModel(model)

        # controls
        self.ui_table = QtGui.QTableView()
        self.ui_table.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.ui_table.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self.ui_table.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
        self.ui_table.setModel(proxy_model)
        self.ui_table.setSortingEnabled(False)
        self.ui_table.setSortingEnabled(True)
        self.ui_table.sortByColumn(0, self.ui_table.horizontalHeader().sortIndicatorOrder())

        self.ui_search = QtGui.QLineEdit()
        self.ui_selected = QtGui.QPushButton('Select Emily')

        # lay main
        lay_main = QtGui.QVBoxLayout()
        lay_main.addWidget(self.ui_search)
        lay_main.addWidget(self.ui_table)
        lay_main.addWidget(self.ui_selected)
        self.setLayout(lay_main)

        # connections
        self.ui_selected.clicked.connect(self.clicked_selected_emily)
        self.ui_search.textChanged.connect(self.filter_items)


    def clicked_selected_emily(self):
        print 'select emily'
        self.ui_table.selectRow(2)

    def filter_items(self, text):
        rows = self.ui_table.model().rowCount()
        for row in range(rows):
            self.filter_row(row, text)


    def filter_row(self, row, pattern):
        if not pattern:
            self.ui_table.setRowHidden(row, False)
            return

        model = self.ui_table.model()
        columns = model.columnCount()
        stringlist = []

        # collect text from all columns into single string for searching
        for c in range(columns):
            mdx = model.index(row, c)
            if mdx.isValid():
                val = str(mdx.data(role=QtCore.Qt.DisplayRole)).lower()
                stringlist.append(val)

        # search for string
        patterns = filter(None, [x.lower() for x in pattern.split(' ')])
        results = all(any(x in y for y in stringlist) for x in patterns)
        if results:
            self.ui_table.setRowHidden(row, False)
        else:
            self.ui_table.setRowHidden(row, True)



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

Solution

  • You have to use the match() method of the model:

    def clicked_selected_emily(self):
        print("select emily")
        self.ui_table.clearSelection()
        indexes = self.ui_table.model().match(
            self.ui_table.model().index(0, 0),
            QtCore.Qt.DisplayRole, # role of the search, the text uses the role Qt::DisplayRole
            "Emily", # value that is being searched in the model.
            -1, # maximum number of values ​​are being searched, if it is -1 it will search for all the matches
            QtCore.Qt.MatchExactly # type of search
        )
        for ix in indexes:
            self.ui_table.selectRow(ix.row())
    

    Update:

    By default, the search is the column that is passed to self.ui_table.model().index(0, col) in the previous example, if you want to search through all the columns you should only iterate over them, to observe the effect, multiselection is enabled:

        self.ui_table.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
    
    ...
    
    def clicked_selected_emily(self):
        print("select banker")
        self.ui_table.clearSelection()
        word = "banker"
    
        for i in range(self.ui_table.model().columnCount()):
            indexes = self.ui_table.model().match(
                self.ui_table.model().index(0, i),
                QtCore.Qt.DisplayRole, # role of the search, the text uses the role Qt::DisplayRole
                "banker", # value that is being searched in the model.
                -1, # maximum number of values ​​are being searched, if it is -1 it will search for all the matches
                QtCore.Qt.MatchExactly # type of search
            )
            for ix in indexes:
                self.ui_table.selectRow(ix.row())
    

    enter image description here