Results 1 to 2 of 2

Thread: Retrieve selected indices from a QListWidget

  1. #1
    Join Date
    May 2008
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Retrieve selected indices from a QListWidget

    If the user has selected multiple items in a QListWidget (contiguously or otherwise), is there any way to get a list (std::vector, preferably, or, better yet, an std::vector<bool> with 1's in the appropriate indices) of the selected rows without doing something like the following?

    Qt Code:
    1. QList<QListWidgetItem*> selection = listWidget->selectedItems();
    2. int S = selection.size();
    3. std::vector<int> indices;
    4. for(int i = 0; i < S; i++) indices.push_back(listWidget->row(selection[i]));
    To copy to clipboard, switch view to plain text mode 

    virtual QModelIndexList QAbstractItemView::currentIndexes()

    looks like it might be what I want, but it doesn't seem to be re-implemented for QListWidget or QListView

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Retrieve selected indices from a QListWidget

    If you want a list of selected indexes, try this

    Qt Code:
    1. QModelIndexList indexes = listWidget->selectionModel()->selectedIndexes();
    2.  
    3. std::vector<int> indexList;
    4. foreach(QModelIndex index, indexes)
    5. {
    6. indexList.push_back(index.row());
    7. }
    To copy to clipboard, switch view to plain text mode 

    For a list of boolean, try this

    Qt Code:
    1. QItemSelectionModel* selectionModel = listWidget->selectionModel();
    2. QAbstractListModel* model = qobject_cast<QAbstractListModel*>(listWidget->model());
    3.  
    4. if(model == 0)
    5. {
    6. QMessageBox::critical(this, "Error", "Wrong conversion");
    7. }
    8. std::vector<bool> indexes;
    9. bool selected = false;
    10. for(int row = 0; row < model->rowCount(); ++row)
    11. {
    12. selected = selectionModel->isSelected(model->index(row));
    13. indexes.push_back(selected);
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mcosta; 5th June 2008 at 14:12. Reason: missing [code] tags
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. qlistwidget count of selected items
    By tpf80 in forum Newbie
    Replies: 4
    Last Post: 10th December 2009, 13:31

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.