PDA

View Full Version : QTableView item selected signal?



sherbs
30th July 2010, 13:41
Hi Folks...

I'm having an issue figuring out what signals are emitted by a QTableView when the user performs various actions for selecting a row (my table is configured for row selection mode, single selection)...

I'm connecting to the clicked() signal and can pick up on row selection when the user clicks on a row - all fine and well. However, if the user then uses the up/down arrow keys to select new rows, the clicked() signal is not emitted - as I suppose would be expected.

Why is it that there just doesn't seem to be a signal letting us know that the set of selected items has changed? Am I just overlooking this somewhere in the docs?

Thanks!

Btw, I'm using Qt 4.6 on Windows

-G

saa7_go
30th July 2010, 14:38
Am I just overlooking this somewhere in the docs?


Yup, look at QAbstractItemView::selectionModel() (http://doc.trolltech.com/4.6/qabstractitemview.html#selectionModel) and QItemSelectionModel (http://doc.trolltech.com/4.6/qitemselectionmodel.html).

sherbs
30th July 2010, 19:54
Thanks... that helps a lot!

sherbs
2nd August 2010, 14:40
I finally got around to implementing this and ran into an unexpected issue:

QObject::connect: Cannot connect (null)::currentRowChanged(QModelIndex,QModelIndex) to MainWindow::on_tableViewTriggerSelectionModel_curr entRowChanged(QModelIndex,QModelIndex)

My code for doing the connection is as follows:


QItemSelectionModel *sm = ui->tableViewTrigger->selectionModel();
connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
this, SLOT(on_tableViewTriggerSelectionModel_currentRowC hanged(QModelIndex,QModelIndex)));


Any idea why the selection model is coming back as null? I'm using QtCreator to design/build... in that I'm setting selectionMode to SingleSelection and selectionBehavior to SelectRows

Do I need to explicitly create the selection model?

Thanks for any help!

saa7_go
2nd August 2010, 15:08
QItemSelectionModel *sm = ui->tableViewTrigger->selectionModel();
connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
this, SLOT(on_tableViewTriggerSelectionModel_currentRowC hanged(QModelIndex,QModelIndex)));


I guess you set the model after the code above. Try to set the model first.



ui->tableViewTrigger->setModel(yourModel);
QItemSelectionModel *sm = ui->tableViewTrigger->selectionModel();
connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
this, SLOT(on_tableViewTriggerSelectionModel_currentRowC hanged(QModelIndex,QModelIndex)));

sherbs
2nd August 2010, 17:46
Here is the order in which things are happening (simplified somewhat for clarity):


MainWindow::MainWindow(QWidget *parent)
{
ui->setupUi(this);
this->_initializeTables();
}


setupUi() is doing the following:


tableViewTrigger->setSelectionMode(QAbstractItemView::SingleSelectio n);
tableViewTrigger->setSelectionBehavior(QAbstractItemView::SelectRows );


and my initialization routine for my tables does this:


void MainWindow::_initializeTables()
{

QItemSelectionModel *sm = ui->tableViewTrigger->selectionModel();
connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
this, SLOT(on_tableViewTriggerSelectionModel_currentRowC hanged(QModelIndex,QModelIndex)));
}

I was under the impression that setting the SelectionMode and SelectionBehavior would be something that manipulated a default sort of SelectionModel...

Apparently these two things are not the same and that a SelectionModel must be created by the programmer to do this? In a way, I find that kind of surprising since selecting items from a view of any sort is a common task.

Lykurg
2nd August 2010, 18:23
You don't have to create a selection model by yourown. Qt does it as soon as you set a (data)model to the view. Qt does it at that time because if the view is empty no selection model is needed. As soon as it is needed it creates one with you specified mode and behavior.

sherbs
3rd August 2010, 15:02
Thanks much!

That was the problem...

I was setting the model for the table view *after* making that connection... Setting the model prior did the trick.

-G