PDA

View Full Version : QTableView and selectionModel problem



unix7777
10th September 2012, 18:38
Hi,

I have a QWidget with QTableView inside.When add button is clicked it opens a dialog.When dialog is closed it updates the data editing the records.
I use the signal:

connect(ui->tableView_clients->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelect ion)),this, SLOT(setBool()));
to check whether row was selected and if is true it enables the button because otherwise the app crushes.
The problem is that when i update the model this signal doesn't work.What i mean: when i open the widget everything works properly, when row is selected the button becomes enabled when is not selected become disabled.But, when i open the dialog and update the model editing some of the records and close the dialog the buttons remain only enabled or only disabled which means that selectionModel is not available or something like that.

HELP PLEASE

pkj
11th September 2012, 06:10
You need to rephrase your question. At least I am not able to get the picture. I think you are implying that selectionChanged() is not getting emitted. As you might know selectionChanged() only works on qmodelindexes. If the data pointed by qmodelindex is changed, selectionChanged() will not be emitted.
You should rather look at the dataChanged() of the model.

unix7777
15th September 2012, 10:01
OK, i'll try to make my question clearer:


connect(ui->tableView_clients->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelect ion)),this, SLOT(setBool()));
i use selectionChange() to check in setBool() wether to make buttons disabled or enabled. Generally it works, but when i add or edit record i run the following function in order to update the table:

//UPDATE MAIN TABLEVIEW
void Clients::updateClientTable()
{

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 id, ClientName,ClientCity, ClientAddress, ClientMol, ClientEik, ClientVat, ClientTel, ClientMail FROM clients");
model->setHeaderData(0, Qt::Horizontal, QObject::tr("id"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("City"));
model->setHeaderData(5, Qt::Horizontal, QObject::tr("EIK"));


ui->tableView_clients->setModel(proxyModel);
//ui->tableView_clients->setSortingEnabled(true);
ui->tableView_clients->setColumnWidth(0,20);//ID
ui->tableView_clients->setColumnWidth(1,173);//ClientName
ui->tableView_clients->setColumnWidth(2,110);//ClientCity
ui->tableView_clients->setColumnWidth(3,0);//ClientAddress
ui->tableView_clients->setColumnWidth(4,0);//ClientMol
ui->tableView_clients->setColumnWidth(5,100);//ClientEik
ui->tableView_clients->setColumnWidth(6,0);//ClientVat
ui->tableView_clients->setColumnWidth(7,0);//ClientTel
ui->tableView_clients->setColumnWidth(8,0);//ClientMail
ui->tableView_clients->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
ui->tableView_clients->horizontalHeader()->setStretchLastSection(true);
ui->tableView_clients->show();

Unfortunately after running it the signal selectionChanged() doesn't work anymore.I select different rows but nothing happens.

pkj
18th September 2012, 06:00
Go through the documentation of QAbstracitItemView::setModel(). It states very clearly that each time you do that, you create a new selection model. qabstractitemview.html#setModel (http://doc.qt.nokia.com/4.7-snapshot/qabstractitemview.html#setModel)
So you cannot expect your old connect to work anymore! Create a new connect, read the documentation and follow it.