One row chosen in treeview, selectionModel()->currentIndex() gives one index, selectionModel()->selectedIndexes() gives four?



  • I have a QTreeView that I'm modifying the behaviour of. Currently, there is a right-click context option that allows selection of one item in the QTreeView. The user right-clicks, and picks the appropriate option from the context menu, and the following line of code is triggered.
    @ auto index = ui->componentMessageTreeView->selectionModel()->currentIndex();@
    I need to get more than one QModelindex now, so the user can now select multiple rows and I have added the following line of code directly after the first:
    @ QModelIndexList list = ui->componentMessageTreeView->selectionModel()->selectedIndexes();@

    I decided to check that when the user selects only one row in the treeview, the list contained one QModelIndex item, and that it was the same as the index object created on the previous line of code. The code list.size() returns the value 4. The list has four items in it. I expected it to contain only one, because I selected only one in the QTreeView. This makes no sense to me, so I must have misunderstood something pretty fundamental. What have I missed?



  • How many columns you have?

    If you have 4 columns then the list will contain those 4 indices in a row.



  • I think you're right; I thought there were only three columns, but it seems that there is some kind of vestigial forth column hanging around, but made invisible to the user.

    So when I use @selectionModel()->currentIndex();@ I do get only a single index (one "cell" of the layout, so to speak), but when I use@ selectionModel()->selectedIndexes();@ I get the whole row; I think I didn't understand clearly what was meant by "current" and what was meant by "selection"; when I click on a given "cell" in the QTreeView, the entire row is "selected" but only a single cell is the current index.



  • have you noticed there are two separate things the selection mode and behavior? may be this does what you have expected:

    view.setSelectionMode(QAbstractItemView::ExtendedSelection); view.setSelectionBehavior(QAbstractItemView::SelectItems);

    current is always the only item where you clicked ...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.