Results 1 to 5 of 5

Thread: QListView::selectedIndexes() comes back in the wrong order when in IconMode

  1. #1
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default QListView::selectedIndexes() comes back in the wrong order when in IconMode

    I'm using a QListView in icon mode but the selection is coming back in the wrong order. Is this a Qt bug or am I missing a setting on my QListView to account for this strange behavior?
    Try running the code below in a python interpreter, shift select item 0 - item 40 and then print out the order of the selected items.

    PyQt example:

    Qt Code:
    1. from PyQt4 import QtGui
    2.  
    3. app = QtGui.QApplication([])
    4. view = QtGui.QListWidget()
    5. view.setViewMode(QtGui.QListView.IconMode)
    6. view.setSelectionMode(QtGui.QListView.ExtendedSelection)
    7.  
    8. for i in range(100):
    9. view.addItem(QtGui.QListWidgetItem(str(i)))
    10.  
    11.  
    12. view.show()
    13. # select items 0-40
    14. for item in view.selectedItems():
    15. print item.text()
    To copy to clipboard, switch view to plain text mode 
    You'll get a result like this:
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 16, 26, 17, 27, 18, 19, 28, 29, 20, 30, 21, 31, 22, 32, 23, 33, 34, 35, 36, 37, 38, 39, 40

    If the QListView is set to ListMode then the items come back in the expected order. And in case 'the expected order' is too vague, I would expect them to come back in numeric order, the same as ListMode.
    Last edited by chezifresh; 1st December 2011 at 23:50.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QListView::selectedIndexes() comes back in the wrong order when in IconMode

    Where does the Qt documentation say anything about the order of indexes in the selection?

  3. #3
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: QListView::selectedIndexes() comes back in the wrong order when in IconMode

    I'd imagine its implicit. If you drag and drop something from one part of your view to another you'd expect that when you drop them, they would appear in the same order that you dragged them. Are you saying that any time you drag more than one thing in Qt the order in the drag object is non-deterministic? I doubt that was the intent.

    If you drag multiple items in a list view or a tree view and drop them on the Qt example 'dropsite', they will appear in the QMimeData in the same order in which they are displayed.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QListView::selectedIndexes() comes back in the wrong order when in IconMode

    Drag and drop is entirely within programmer control. The drag payload is constructed by the sender and can be structured or ordered any way the sender desires, and interpreted however the receiver likes. It is entirely determined by the programmer.

    Selections are also deterministic: you always get all the indexes of selected items and if you query the same selection twice you get the same result. That they are not in the 'expected order' is not an indication of a random process.

    A selection can be built up by adding single items, ranges of items, overlapping ranges, disjoint ranges, items under an arbitrary rectangular area in a view displaying the items a variable layout, selections made while the view is sorted in different orders, all merged into a compact representation of the selection. There is no single notion of the 'correct' or 'natural' order that makes sense for all: is it the order of insertion in the list, alphabetical or numerical order of the DisplayRole (there may be none), row or column major for a table, pre- or post-order for a tree, order that the user selected the fragments of the selection, order of the top-left corner of section fragments...

    If you want the items in the selection sorted then you are free to sort them yourself. Alternatively, you could iterate the list in your 'expected order' and query if each index is in the selection rather than relying on the selection list being in some order.

  5. #5
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: QListView::selectedIndexes() comes back in the wrong order when in IconMode

    Thanks for the answer, I totally agree. I think I was a bit thrown by the fact that changing view modes in the QListWidget would change the selectedItems() order.

    I fixed this entirely in 2 places. First I overloaded selectedItems:

    Qt Code:
    1. def selectedItems(self):
    2. indexes = self.selectionModel().selectedIndexes()
    3. indexes.sort(cmp=lambda a, b : cmp(a.row(), b.row()))
    4. items = [self.itemFromIndex(index) for index in indexes]
    5. return items
    6.  
    7. def mimeData(self, items):
    8. mimedata = QtCore.QMimeData()
    9. ordered_items = []
    10. for idx in range(self.count()):
    11. item = self.item(idx)
    12. if item in items:
    13. ordered_items.append(item)
    14. ...
    To copy to clipboard, switch view to plain text mode 

    the mimeData implementation is not exactly optimized, but for some reason QListWidgetItems dont seem to have an index() method to get their model index. Otherwise I would also sort them by their rows

Similar Threads

  1. Replies: 0
    Last Post: 16th October 2011, 02:27
  2. Caching icons in QListView::IconMode
    By slash_blog in forum Qt Programming
    Replies: 2
    Last Post: 1st September 2011, 14:13
  3. Replies: 4
    Last Post: 5th June 2011, 20:59
  4. QTextStream input and output executing in wrong order
    By jrmrjnck in forum Qt Programming
    Replies: 6
    Last Post: 8th November 2010, 16:33
  5. Replies: 2
    Last Post: 2nd June 2008, 08: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.