[Solved] Signal when a QListView selection changes due to keyboard activity?
-
I have a QDialog, created with QT Designer, that looks like so:
!http://i.imgur.com/1XdmGvT.png()!The list of servers on the left is a QListView with a QStringListModel. Mouse clicking an item in the list view updates the form with the information for the selected item by connecting the view’s activated(QModelIndex) signal to a slot function in the dialog.
However, pressing up or down on the keyboard also changes the selected item, but no signal is emitted, so the form isn't updated to match the selected item. How can this be fixed?
I'm using Qt5
-
Hi DrTwox!
For this cases you should use QItemSelectionModel signals.
@QItemSelectionModel *selectionModel = ui->listView->selectionModel();
connect(selectionModel,SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
this,SLOT(rowChangedSlot(QModelIndex,QModelIndex)));@ -
Thanks for the answer qxoz, but I get the following error at runtime when using that code:
QObject::connect: No such signal QItemSelectionModel::currentRowChanged(QItemSelection,QItemSelection)
-
Strange it should work. Do you write exactly as i show?
Not currentRowChanged(QItemSelection,QItemSelection) but currentRowChanged(QModelIndex,QModelIndex) ? -
Oops, I did have that wrong! Thanks.
However, now the runtime can't find the slot function. What should it look like? I have:
@void ConnectionDialog::rowChangedSlot( const QModelIndex& selected, const QModelIndex& deselected ) {}
@
... but at runtime I getQObject::connect: No such slot ConnectDialog::rowChangedSlot(QModelIndex,QModelIndex)
Here's the connect call:
@connect( serverListView->selectionModel(), SIGNAL( currentRowChanged(QModelIndex,QModelIndex)), this, SLOT( rowChangedSlot(QModelIndex,QModelIndex) ) );@
-
I forgot to put the slot function under the private slots: section in the header. All is working now. Thank you for the help!
-
Great!
If it solved please add to the title [Solved].