PDA

View Full Version : Problem with QTableView and QSortFilterProxyModel



unix7777
19th August 2012, 20:50
Hi,

i have a model an tableview.
I set the model to the table view on the following way:

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));
proxyModel->setFilterKeyColumn(-1);

connect(ui->lineEdit_search_clients, SIGNAL(textChanged(QString)),
proxyModel, SLOT(setFilterFixedString(QString)));
connect(proxyModel, SIGNAL(hasSelection()),
ui->lineEdit_search_clients, SLOT(setText()));


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();

When i search using QSortFilterProxyModel the content in the TableView changes it's position and doesn't map the model.
Than when i try to select some row , what i see doesn't respond of what i select.

This is the selection:


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();

Please HELP!!!

StrikeByte
21st August 2012, 13:47
To get the correct items from the model you will have to use proxyModel->mapToSource() else you will get the row numers that the proxy uses

unix7777
21st August 2012, 19:42
I don't know in which of the code i should use it?The proxyModel is declared in updateCleintTable()
and i want to get the row value in on_pushButton_select_clicked()

StrikeByte
23rd August 2012, 13:34
can you attach the full code then it is easyer to explain

form the previous piece of code you should change your selecion code


selected_row=proxyModel->mapToSource(selectedList.at(i)).row();

change proxyModel to the name of your proxy model

unix7777
23rd August 2012, 14:04
Thank you for answer so quick.
The proxyModel is declared in another place.

This is the code:


void Clients::updateCleintTable()
{
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));
proxyModel->setFilterKeyColumn(-1);

connect(ui->lineEdit_search_clients, SIGNAL(textChanged(QString)),
proxyModel, SLOT(setFilterFixedString(QString)));
connect(ui->lineEdit_search_clients, SIGNAL(textChanged(QString)),
ui->tableView_clients, SLOT(setModel(proxyModel)));



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();

}


And another one:



void Clients::on_pushButton_select_clicked()
{

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();


}

StrikeByte
23rd August 2012, 14:40
Then ask the view for the proxymodel
ui->tableView_clients->model(); //this will return the proxy model that you set in your view

unix7777
23rd August 2012, 19:06
Thank you for the answer, but is seems that ui->tableView_clients->model() doesn't return the proxyModel because there is no function mapToSource()

Added after 24 minutes:

I just have write this

mod->setSourceModel(ui->tableView_clients->model());
and it works, i just have to change the indexes of the cells.

THANK YOU VERY MUCH FOR YOUR HELP!!!

StrikeByte
24th August 2012, 08:11
mod->setSourceModel(ui->tableView_clients->model()); this is not the right way to do it

ui->tableView_clients->model() returns a QAbstractModel you have to cast it into a QProxyModel like this:


QProxyModel * tmpProxy;
if(tmpProxy = qobject_cast<QProxyModel *>(ui->tableView_clients->model()))
{
// here you can use the tmpProxy to mapToSource
}