Problem with QTableView and QSortFilterProxyModel
Hi,
i have a model an tableview.
I set the model to the table view on the following way:
Code:
QString searchValue
= ui
->lineEdit_search_clients
->text
();
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:
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();
Please HELP!!!
Re: Problem with QTableView and QSortFilterProxyModel
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
Re: Problem with QTableView and QSortFilterProxyModel
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()
Re: Problem with QTableView and QSortFilterProxyModel
can you attach the full code then it is easyer to explain
form the previous piece of code you should change your selecion code
Code:
selected_row=proxyModel->mapToSource(selectedList.at(i)).row();
change proxyModel to the name of your proxy model
Re: Problem with QTableView and QSortFilterProxyModel
Thank you for answer so quick.
The proxyModel is declared in another place.
This is the code:
Code:
void Clients::updateCleintTable()
{
QString searchValue
= ui
->lineEdit_search_clients
->text
();
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:
Code:
void Clients::on_pushButton_select_clicked()
{
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();
}
Re: Problem with QTableView and QSortFilterProxyModel
Then ask the view for the proxymodel
ui->tableView_clients->model(); //this will return the proxy model that you set in your view
Re: Problem with QTableView and QSortFilterProxyModel
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
Code:
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!!!
Re: Problem with QTableView and QSortFilterProxyModel
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:
Code:
if(tmpProxy = qobject_cast<QProxyModel *>(ui->tableView_clients->model()))
{
// here you can use the tmpProxy to mapToSource
}