how to search using QSortFilterProxyModel
-
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)
-
@LT-K101
Although I cannot actually see it documented, the error messageQTableWidget.setModel() is a private method
tells you that you cannot callsetModel()
directly on aQTableWidget
. 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 aQSortFilterProxyModel
you probably need to move to aQTableView
.See also https://stackoverflow.com/questions/1137732/setting-the-model-to-a-qtablewidget
-
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.
-
@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
-
You are mentioning database, are you using a SQL database ?
-
So the next question is whether you want the database to do the filtering or the proxy model.
-
@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.