10

I'm trying to create a filter for QTableWidget with QLineEdit in PySide. I've seen some tutorials using QSortFilterProxyModel for C++ but couldn't understood how to do it in Python.

enter image description here

I need to search in 'VALUE' column.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
snir.tur
  • 461
  • 3
  • 7
  • 14
  • 2
    All the Qt examples have been converted to Python. They are included in the source packages of PyQt (or PySide), which you can download [here](https://riverbankcomputing.com/software/pyqt/download). Look for the `basicsortfiltermodel.py` example. – titusjan Dec 13 '15 at 18:09
  • [PyQt5 examples](https://github.com/baoboa/pyqt5/tree/master/examples), [PyQt4 examples](https://github.com/Werkov/PyQt4/tree/master/examples). – ekhumoro Aug 08 '19 at 11:16

1 Answers1

16

A QSortFilterProxyModel is a proxy model, that means that you put it between the your complete data model and a view. The comment by titusjan is good, you can look in your local PySide/PyQt installation for basicsortfiltermodel.py to get an example in Python.

Also, instead of using a QTableWidget a QTableView is sufficient - you won't need the inbuilt model of QTableWidget anyway.

QTableWidget:Details

The QTableWidget class provides an item-based table view with a default model.

Table widgets provide standard table display facilities for applications. The items in a QTableWidget are provided by QTableWidgetItem.

If you want a table that uses your own data model you should use QTableView rather than this class.

I compiled an very simple example demonstrating filtering for the third column of a QTableView:

from PySide import QtCore, QtGui

app = QtGui.QApplication([])
window = QtGui.QWidget()

# standard item model
model = QtGui.QStandardItemModel(5, 3)
model.setHorizontalHeaderLabels(['ID', 'DATE', 'VALUE'])
for row, text in enumerate(['Cell', 'Fish', 'Apple', 'Ananas', 'Mango']):
    item = QtGui.QStandardItem(text)
    model.setItem(row, 2, item)

# filter proxy model
filter_proxy_model = QtGui.QSortFilterProxyModel()
filter_proxy_model.setSourceModel(model)
filter_proxy_model.setFilterKeyColumn(2) # third column

# line edit for filtering
layout = QtGui.QVBoxLayout(window)
line_edit = QtGui.QLineEdit()
line_edit.textChanged.connect(filter_proxy_model.setFilterRegExp)
layout.addWidget(line_edit)

# table view
table = QtGui.QTableView()
table.setModel(filter_proxy_model)
layout.addWidget(table)

window.show()
app.exec_()

You have a QStandardItemModel which is set as source of a QSortFilterProxyModel which uses the third column for filtering and uses the input of a QLineEdit as filtering expression. The QSortFilterProxyModel is used as model by a QTableView.

And it looks like:

No filtering

Filtering

Community
  • 1
  • 1
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
  • Thanks for your answer, I cant use the table.setModel method since i'm using it in calss and its private method, what can I do about it? – snir.tur Dec 15 '15 at 13:08
  • I changed my table to QTableView and it works! but there is a way to do it with QTableWidget? – snir.tur Dec 15 '15 at 13:38
  • 2
    @snir.tur You cannot use a QTableWidget because it has a non-changeable inbuilt model. QTableView is the perfect substitution. Either use it or you cannot do what you want. – NoDataDumpNoContribution Dec 15 '15 at 14:23
  • How do you filter for WHOLE word? For example you have "A" and get "Apple" and "Ananas", I want it to have to be = "Apple". – Scott Ulmer Nov 30 '19 at 08:01