Reputation: 195
In my application, I have QTableWidget
displaying multiple rows, a line edit to enter a string and a push button.
The requirement says, upon clicking on the push button, the same QTableWidget
should show only rows which contain the string entered into the line edit.
I thought of using a QSortFilterProxyModel
, but QTableWidget
has setModel(...)
method private, so I am unable to use QSortFilterProxyModel
in this case.
Upvotes: 15
Views: 27088
Reputation: 3125
I HIGHLY recommend going about this in the following way! This is how it is meant to be done in Qt.
Look at the tutorial on Qt Model/View Programming. The problem is that QTableWidget is a convenience class that hides the Model/View stuff for you. In your case, you can't (or shouldn't) ignore the Model/View structure Qt provides.
What you will need to do:
QTableView
instead of a QTableWidget
.QAbstractItemModel
and implement data()
(for reading), and all the other functions you need from the documentation. This is the trickiest part, but refer to the above link for a walkthrough of how to do this.QSortFilterProxyModel
and setModel()
of the QTableView
to it.setSourceModel()
of your QSortFilterProxyModel
to your subclassed model.setFilterFixedString()
or setFilterRegExp()
in your QSortFilterProxyModel
Let me know if this helps. This is far more professional, and in the long run, elegant, than iterating through all the elements in your table.
Upvotes: 27
Reputation: 17535
Using a sort/filter proxy is probably overkill for this anyway.
It's a matter of iterating through all of your QTableWidgetItem objects, determining if their text matches the filter and calling QTableView::setRowHidden() as needed.
For example:
QString filter = textEdit->text();
for( int i = 0; i < table->rowCount(); ++i )
{
bool match = false;
for( int j = 0; j < table->columnCount(); ++j )
{
QTableWidgetItem *item = table->item( i, j );
if( item->text().contains(filter) )
{
match = true;
break;
}
}
table->setRowHidden( i, !match );
}
Upvotes: 30