PDA

View Full Version : QSortFilterProxyModel nothing changes



unix7777
17th August 2012, 08:53
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


QString searchValue = ui->lineEdit_search_clients->text();

QSqlQueryModel *model=new QSqlQueryModel();

QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel;
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();

yeye_olive
17th August 2012, 09:59
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.

unix7777
17th August 2012, 16:36
Thanks, now the code looks like this:


QString searchValue = ui->lineEdit_search_clients->text();
QSqlQueryModel *model=new QSqlQueryModel(this);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);

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?

yeye_olive
17th August 2012, 16:42
QSortFilterProxyModel has a method to select which column to filter: either a specific one or all of them.

unix7777
17th August 2012, 17:31
sorry , but i can't figure out how to filter ALL without subclassing!

ecanela
18th August 2012, 07:38
add the code

proxymodel->setFilterKeyColumn ( -1 )

the -1 indicate read the value in all the column.

unix7777
18th August 2012, 09:26
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:

QModelIndexList selectedList = ui->tableView_clients->selectionModel()->selectedRows();

QSqlQueryModel *mod=new QSqlQueryModel(this);


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.

unix7777
19th August 2012, 08:13
Does nobody have an idea why the model doesn't correspond to the view after searching with QSortFilterProxyModel?