Difficulties with selecting items in a QTreeView

11

Scenario:

I have an application that manages a list of images of human faces with prototypical emotional expressions.

I've created a class inherited from QAbstractListModel to provide the data access model (model), and I'm using two native Qt view classes for display the list to the user: QListView to display "icons" (thumbnails of images in the list) and QTreeView to display details (tabular format, with image name and emotional label). >

Both view classes use the same select control (a default instance of QItemSelectionModel created with reference to the template mentioned above). Thus, when the user clicks / selects one or more images in one view the selection is reproduced in the other.

The instance of QTreeView (all components have been added in Qt Designer and are loaded in the .ui file) is set (in the selectionBehavior property) for selection by lines, as shown below:

Problem:

TheproblemisthatalthoughtheQTreeViewcomponentiscorrectlydisplayingtherow-wideselectionwhentheuserinteractswithit,thecomponentstillselectstheitemsindividually(andbyitem,understandanycellinarowandcolumn).So,forexample,iftheuserselectstwoitemsintheQListViewview,changetotheQTreeViewviewandselectanotheritem(holdingtheCtrlkey)byclickingthesecondcolumn,thevisualresultisasfollows:

Notice how in the view of QTreeView (right) the cell of the 4th line 2nd column has a subtle border indicating the last selected item, and the other lines (which were originally selected in the left view) do not have the line fully selected.

In addition to the visual problem, there is also a more serious problem when processing the selected items. The code below, for example, is used to remove the selected images from the current file. Ideally, three (3) images are selected (Angelina, Brad and Scarlett), but the selectedIndexes() call on the selection management object returns four (4) items (since it individually looks at the cells).

ChildWindow *pChild = (ChildWindow*) ui->tabWidget->currentWidget();
if(!pChild)
    return;

QModelIndexList lsSelected = pChild->getSelectionModel()->selectedIndexes();
if(lsSelected.size() > 0)
{
    QString sMsg;
    if(lsSelected.size() == 1)
        sMsg = tr("Você confirma a remoção da imagem selecionada?");
    else
        sMsg = tr("Você confirma a remoção das %1 imagens selecionadas?").arg(lsSelected.size());
    if(QMessageBox::question(this, tr("Confirmação da remoção"), sMsg, QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes)
    {
        vector<int> vIndexes;
        for(int i = 0; i < lsSelected.size(); i++)
            vIndexes.push_back(lsSelected[i].row());
        pChild->removeImages(vIndexes);
        updateUI();
    }
}

IgottotestusingtheselectedRows()callinsteadofselectedItems(),buttheproblemcontinues(albeitslightlydifferent).ThenumberofrowsonlyconsidersthosefullyselectedthroughtheviewofQTreeViewandalwaysignoresselecteditems"partially" by means of QListView .

Anyway, the question is: how do I force QTreeView to always display the fully selected rows and ensure that only the items in the first column are actually selected?

    
asked by anonymous 04.03.2015 / 04:35

1 answer

6

You must set the selectionBehavior property of the ListView to the SelectRows value. If not, ListView spoils your selection.

For those who want to test, use the example interview included in QtCreator. Add the following line in the main.cpp file:

list->setSelectionBehavior(QAbstractItemView::SelectRows);
    
06.03.2015 / 20:19