PDA

View Full Version : qt4 - iterating thru QListView



incapacitant
14th March 2006, 11:04
I want iterate on a QListView and check if a row is selected.
With QT3 there was:


QListBoxItem *item = qDestin->item( i );
if ( item->isSelected() )
ret.push_back( item->text() );



What is the equivalent in QT4 ?

I know there is selectedItems in QTreeVidget but I don't know what to do with this QListModelIndex returned !

jpn
14th March 2006, 11:29
I want iterate on a QListView and check if a row is selected.

I know there is selectedItems in QTreeVidget but I don't know what to do with this QListModelIndex returned !
So which one are you using? QListView or QTreeWidget?

QTreeWidget provides a method for getting all selected items:

QList<QTreeWidgetItem *> selectedItems () const

Or optionally if you want to have more control over what kind of items you want by changing the combination of flags:


// QTreeWidget* tree
QTreeWidgetItemIterator::IteratorFlags flags = QTreeWidgetItemIterator::Selected | QTreeWidgetItemIterator::Checked;
for (QTreeWidgetItemIterator iter(tree, flags); *iter; iter++)
{
QTreeWidgetItem* item = *iter;
// do something with your item
}

wysota
14th March 2006, 12:25
Method 1a and 1b:

QListView *lv;
//...
QItemSelectionModel *sel = lv->selectionModel();
bool third = sel->isRowSelected(3, lv->rootIndex()); // check if row "3" is selected
QModelIndexList list = sel->selectedIndexes();
foreach(QModelIndex index, list){
qDebug("Row %d selected", index.row());
}

Method 2:

QListView *lv;
//...
QModelIndexList list = lv->selectedItems(); // provided for convenience
foreach(QModelIndex index, list){
qDebug("Row %d selected", index.row());
}

xelag
27th November 2006, 00:38
Method 2:
...
QModelIndexList list = lv->selectedItems(); // provided for convenience
...
}[/code]

selectedItems() function is only possible to use if you have a QListWidget !
With a QListView, you have to use your own QItemSelectionModel as it is shown in method 1.