PDA

View Full Version : Navigate through qlistview items with shortcuts



jiveaxe
8th January 2008, 18:13
Hi and happy new year,
this is my problem:

my mainDialog has a qlistview. When I click on an item a modeless dialog is open showing some informations related to the item. What i want is that pressing, for example, PagDown it shows me informations on the next item of the qlistview (so having the focus on the informationDialog and without clicking on the item).

I have created this event:


void informationDialog::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_PageUp){
emit previousTitle();
}
else if (event->key() == Qt::Key_PageDown){
emit nextTitle();
}
}

and in mainDialog:


void MainDialog::viewTitle(const QModelIndex& current)
{
if(!informationDialog) {
informationDialog = new InformationDialog;
connect(informationDialog, SIGNAL(previuosTitle()), this, SLOT(showPreviuosTitle()));
connect(informationDialog, SIGNAL(nextTitle()), this, SLOT(showNextTitle()));
}
informationDialog->currentSelected(current);
}

Which code I have to put in showPreviuosTitle()?

Thanks

jpn
10th January 2008, 08:03
This forum doesn't work like "Hey, I need a function, could you guys code it for me?" so let's put it this way: what did you try so far?

jiveaxe
10th January 2008, 09:42
This forum doesn't work like "Hey, I need a function, could you guys code it for me?" so let's put it this way: what did you try so far?

I'm sorry if I give this impression, but the problem is that I haven't found in the documentation an hypothetically QListViewItemIterator (as in qt3); there is a QListIterator but it not seems usable in my case. My listview is using as model a QSqlTableModel but i have not found in its documentation an iterator or so.

Searching in the forum I have found this (http://www.qtcentre.org/forum/f-newbie-4/t-qt4-iterating-thru-qlistview-1221.html) discussion, but QItemSelectionModel has

select( const QModelIndex &, QItemSelectionModel::SelectionFlags )
which should work but i don't know how retrieve next/previous QModelIndex of the selected item.

Regards

jpn
10th January 2008, 10:18
See QModelIndex::sibling().

jiveaxe
10th January 2008, 11:28
Thanks jpn, sibling was what I need (I'm italian so sometimes i don't understand the use of a function from its name: I had no idea of what was simling; after you pointed me to it I have recovered a thesaurus to get the meaning of that word and discovered that it was the function I was searching for).

Now the final code:


void MainDialog::showPreviuosTitle()
{
QModelIndex mi = listView->currentIndex();
if(mi.row() > 0) {
listView->setCurrentIndex(mi.sibling(mi.row()-1,mi.column()));
viewTitle(listView->currentIndex());
}
}

void MainDialog::showNextTitle()
{
QModelIndex mi = listView->currentIndex();
if(mi.row() < listView->model()->rowCount() - 1) {
listView->setCurrentIndex(mi.sibling(mi.row()+1,mi.column()) );
viewTitle(listView->currentIndex());
}
}

Thanks