QSortFilterProxyModel nothing changes
Hi i'm trying to filter the content displayed in the TableView getting the string from lineEdit but nothing happens when i write in the lineEdit.
HELP
Code:
QString searchValue
= ui
->lineEdit_search_clients
->text
();
proxyModel->setSourceModel(model);
proxyModel
->setFilterRegExp
(QRegExp(searchValue, Qt
::CaseInsensitive,
QRegExp::FixedString));
model->setQuery("SELECT ROWID, ClientName, ClientCity, ClientEik FROM clients");
model
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("RowID"));
model
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("Name"));
model
->setHeaderData
(2, Qt
::Horizontal,
QObject::tr("City"));
model
->setHeaderData
(3, Qt
::Horizontal,
QObject::tr("ID"));
ui->tableView_clients->setModel(proxyModel);
ui->tableView_clients->setSortingEnabled(true);
ui->tableView_clients->setColumnWidth(0,30);
ui->tableView_clients->setColumnWidth(1,170);
ui->tableView_clients->setColumnWidth(2,100);
ui->tableView_clients->setColumnWidth(3,70);
ui
->tableView_clients
->horizontalHeader
()->setResizeMode
(QHeaderView::Interactive);
ui->tableView_clients->horizontalHeader()->setStretchLastSection(true);
ui->tableView_clients->show();
Re: QSortFilterProxyModel nothing changes
If you write in lineEdit after this code has been run then it will not change anything. You have to call setFilterRegExp() again (or, better yet, setFilterFixedString() in your case) after the lineEdit's text has been modified. Also remember that QSortFilterProxyModel filters column 0 of the model by default, which may or may not be what you want.
By the way your code leaks memory in two places.
Re: QSortFilterProxyModel nothing changes
Thanks, now the code looks like this:
Code:
QString searchValue
= ui
->lineEdit_search_clients
->text
();
proxyModel->setSourceModel(model);
proxyModel
->setFilterRegExp
(QRegExp(searchValue, Qt
::CaseInsensitive,
QRegExp::FixedString));
connect(ui
->lineEdit_search_clients,
SIGNAL(textChanged
(QString)),
proxyModel,
SLOT(setFilterFixedString
(QString)));
model->setQuery("SELECT ROWID, ClientName, ClientCity, ClientEik FROM clients");
model
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("RowID"));
model
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("Name"));
model
->setHeaderData
(2, Qt
::Horizontal,
QObject::tr("City"));
model
->setHeaderData
(3, Qt
::Horizontal,
QObject::tr("ID"));
ui->tableView_clients->setModel(proxyModel);
ui->tableView_clients->setSortingEnabled(true);
ui->tableView_clients->setColumnWidth(0,30);
ui->tableView_clients->setColumnWidth(1,170);
ui->tableView_clients->setColumnWidth(2,100);
ui->tableView_clients->setColumnWidth(3,70);
ui
->tableView_clients
->horizontalHeader
()->setResizeMode
(QHeaderView::Interactive);
ui->tableView_clients->horizontalHeader()->setStretchLastSection(true);
ui->tableView_clients->show();
Remain the problem with filtering not only first column but everywhere.Any suggestions?
Re: QSortFilterProxyModel nothing changes
QSortFilterProxyModel has a method to select which column to filter: either a specific one or all of them.
Re: QSortFilterProxyModel nothing changes
sorry , but i can't figure out how to filter ALL without subclassing!
Re: QSortFilterProxyModel nothing changes
add the code
Code:
proxymodel->setFilterKeyColumn ( -1 )
the -1 indicate read the value in all the column.
Re: QSortFilterProxyModel nothing changes
Thank you!
Thank you!
Added after 5 minutes:
A subquestion of my question:
When i get selected row from the model i use it on the following way:
Code:
QModelIndexList selectedList = ui->tableView_clients->selectionModel()->selectedRows();
int selected_row;
int i=0;
selected_row=selectedList.at(i).row();
mod->setQuery("SELECT * FROM clients");
QString ClientName
= mod
->data
(mod
->index
(selected_row,
0)).
toString();
QString ClientCity
= mod
->data
(mod
->index
(selected_row,
1)).
toString();
QString ClientAddress
= mod
->data
(mod
->index
(selected_row,
2)).
toString();
QString ClientMol
= mod
->data
(mod
->index
(selected_row,
3)).
toString();
QString ClientEik
= mod
->data
(mod
->index
(selected_row,
4)).
toString();
QString ClientVat
= mod
->data
(mod
->index
(selected_row,
5)).
toString();
//QMessageBox::information(this,"", ClientMol);
emit selectedName(ClientName);
emit selectedCity(ClientCity);
emit selectedAddress(ClientAddress);
emit selectedMol(ClientMol);
emit selectedEik(ClientEik);
emit selectedVat(ClientVat);
//emit selectedRaw(QString::number(ClientId));
this->close();
The problem is that when searching the order of appearance is changing and the selected row i get already doesn't much the row selected we see.
Re: QSortFilterProxyModel nothing changes
Does nobody have an idea why the model doesn't correspond to the view after searching with QSortFilterProxyModel?