PDA

View Full Version : How to set filter option in QTableWidget



johnMick
22nd July 2011, 04:55
Hi,
In my application I have QtableWidget displaying multiple rows , line edit to enter string and push button, Requirement says upon clicking on push button Same Qtable Widget should show only those rows which are having string entered into line edit.

I thought of using QSortFilterProxy Model but QTableWidget is having SetModel method private so i am unable to use QSortFilterProxy Model in this case .Please let me know how to implement Filter option in QTable Widget

Santosh Reddy
22nd July 2011, 05:16
You can either migrate to QTableView, and user QSortFilterProxy Model (or)

write a filter function,
1. use QList<QTableWidgetItem *> QTableWidget::findItems() to get all the matching items in the table.
2. iterate through the list and hide all the corresponding row numbers


//example
void sort(QString filter)
{
QList<QTableWidgetItem *> items = tableWidget->findItems(filter, Qt::MatchContains);

for(int i = 0; i < items.count(); i++)
tableWidget->hideRow(items.at(i).row());
}

Note: you need to un-hide the rows at some point, then what?... then write un-hide code some where...

jaca
18th October 2011, 00:49
You can either migrate to QTableView, and user QSortFilterProxy Model (or)

write a filter function,
1. use QList<QTableWidgetItem *> QTableWidget::findItems() to get all the matching items in the table.
2. iterate through the list and hide all the corresponding row numbers


//example
void sort(QString filter)
{
QList<QTableWidgetItem *> items = tableWidget->findItems(filter, Qt::MatchContains);

for(int i = 0; i < items.count(); i++)
tableWidget->hideRow(items.at(i).row());
}

Note: you need to un-hide the rows at some point, then what?... then write un-hide code some where...

when the code is compiled the following error is shown:

error: request for member 'row' in 'items.QList<T>::at [with T = QTableWidgetItem*](i)', which is of non-class type 'QTableWidgetItem* const' mingw32-make[1]: *** [release/detonsis.o] Error 1

It seems that the function "row()" does not return the int value.

ChrisW67
18th October 2011, 01:04
error: request for member 'row' in 'items.QList<T>::at [with T = QTableWidgetItem*](i)', which is of non-class type 'QTableWidgetItem* const' mingw32-make[1]: *** [release/detonsis.o] Error 1

It seems that the function "row()" does not return the int value.

Which part of the error message gave you that impression?

In this case at() returns a pointer to QTableWidgetItem, not a QTableWidgetItem, so you need to dereference the pointer in line 7.

jaca
18th October 2011, 03:03
Which part of the error message gave you that impression?

In this case at() returns a pointer to QTableWidgetItem, not a QTableWidgetItem, so you need to dereference the pointer in line 7.

The error message that prevents compile the code and because of the line 7. The
items.at(i).row() does not return an
int.

The solution would be?


QTableWidgetItem* p = items.at(i);
tableWidget->hideRow(p->row());

I m using Qt 4.7.0 on my WIN 7.

norobro
18th October 2011, 04:03
ChrisW67 has given you the answer.

Let me put it another way: If item.at(i) returns a pointer to a QTableWidgetItem, how would you access the member function row()?

jaca
18th October 2011, 12:19
The following code was compiled and gave me the result I expected.


void DetonSis::sort(QString filter)
{
QTableWidgetItem *p = new QTableWidgetItem();
QList<QTableWidgetItem *> items = deslocadosTable->findItems(filter, Qt::MatchExactly);

for(int i = 0; i < items.count(); i++)
p = items.at(i);
deslocadosTable->hideRow(p->row());
}

norobro
18th October 2011, 14:41
The original code had the wrong operator:
void DetonSis::sort(QString filter)
{
// QTableWidgetItem *p = new QTableWidgetItem();
QList<QTableWidgetItem *> items = deslocadosTable->findItems(filter, Qt::MatchExactly);

for(int i = 0; i < items.count(); i++)
// p = items.at(i);
deslocadosTable->hideRow(items.at(i)->row());
}