Results 1 to 4 of 4

Thread: qt4 - iterating thru QListView

  1. #1
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default qt4 - iterating thru QListView

    I want iterate on a QListView and check if a row is selected.
    With QT3 there was:
    Qt Code:
    1. QListBoxItem *item = qDestin->item( i );
    2. if ( item->isSelected() )
    3. ret.push_back( item->text() );
    To copy to clipboard, switch view to plain text mode 


    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 !

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qt4 - iterating thru QListView

    Quote Originally Posted by incapacitant
    I want iterate on a QListView and check if a row is selected.
    Quote Originally Posted by incapacitant
    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:
    Qt Code:
    1. QList<QTreeWidgetItem *> selectedItems () const
    To copy to clipboard, switch view to plain text mode 

    Or optionally if you want to have more control over what kind of items you want by changing the combination of flags:
    Qt Code:
    1. // QTreeWidget* tree
    2. QTreeWidgetItemIterator::IteratorFlags flags = QTreeWidgetItemIterator::Selected | QTreeWidgetItemIterator::Checked;
    3. for (QTreeWidgetItemIterator iter(tree, flags); *iter; iter++)
    4. {
    5. QTreeWidgetItem* item = *iter;
    6. // do something with your item
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to jpn for this useful post:

    incapacitant (14th March 2006), sunil.thaha (27th November 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qt4 - iterating thru QListView

    Method 1a and 1b:
    Qt Code:
    1. //...
    2. QItemSelectionModel *sel = lv->selectionModel();
    3. bool third = sel->isRowSelected(3, lv->rootIndex()); // check if row "3" is selected
    4. QModelIndexList list = sel->selectedIndexes();
    5. foreach(QModelIndex index, list){
    6. qDebug("Row %d selected", index.row());
    7. }
    To copy to clipboard, switch view to plain text mode 

    Method 2:
    Qt Code:
    1. //...
    2. QModelIndexList list = lv->selectedItems(); // provided for convenience
    3. foreach(QModelIndex index, list){
    4. qDebug("Row %d selected", index.row());
    5. }
    To copy to clipboard, switch view to plain text mode 

  5. The following 2 users say thank you to wysota for this useful post:

    incapacitant (14th March 2006), sunil.thaha (27th November 2006)

  6. #4
    Join Date
    Nov 2006
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt4 - iterating thru QListView

    Quote Originally Posted by wysota View Post

    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.

Similar Threads

  1. how to move item up and down in QListView
    By zhanglr in forum Qt Programming
    Replies: 3
    Last Post: 1st August 2008, 14:39
  2. QListView word wrap
    By serega in forum Qt Programming
    Replies: 17
    Last Post: 30th August 2007, 03:13
  3. QDialog / QListView problem
    By harakiri in forum Qt Programming
    Replies: 1
    Last Post: 10th July 2007, 18:31
  4. Subclass QListView to show two colums in one
    By Mookie in forum Qt Programming
    Replies: 2
    Last Post: 23rd June 2007, 02:12
  5. Keeping focus at bottom of QListView
    By jakamph in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2006, 14:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.