Difficulties with selecting items in a Qtreeview

Asked

Viewed 172 times

11

Setting:

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

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

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

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

inserir a descrição da imagem aqui

Problem:

The problem is that despite the component QTreeView be correctly displaying the selection across the row when the user interacts with it, the component still selects the items individually (and per item understands any cell in a row and column). So, for example, if the user selects two items in the view of QListView, change to the view of QTreeView and select one more item (holding down the key Ctrl pressed) by clicking on the second column, the visual result is as follows:

inserir a descrição da imagem aqui

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

In addition to the visual problem, there is also a more serious one at the time of processing the selected items. The code below, for example, is used to remove the selected images from the current file. Ideally there are three (3) selected images (Angelina, Brad and Scarlett), but the call of selectedIndexes() in the selection management object returns four (4) items (since it considers individual 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();
    }
}

inserir a descrição da imagem aqui

I came to test using the call of selectedRows() instead of selectedItems(), but the problem continues (albeit slightly distinct). The number of lines only considers those fully selected through the view of the QTreeView and always ignores the selected items "partially" through the QListView.

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

  • Tried to reset the model when transitioning? (I’m just kicking, I didn’t test). I don’t promise, but if there’s a little time left later I try to reproduce the problem here to try to warm up the brains in search of solution too :)

  • @Bacco You say the transition of views right? I just took the test, but how did I imagine the reset in the model makes him lose the whole selection (after all, it was all restarted right?). Thanks for the help. :)

  • What is your Listview configuration? The selectionBehavior is as SelectRows?

  • @Marcoszolnowski Boy, hit the nail! That’s right. : ) Damn, like the ListView always displays items I didn’t even consider that it also would have this setting (I didn’t mind that this is a property inherited from QAbstractItemView and so it goes for both, even the list not displaying the other columns). Send as answer I accept and I give you the reward. Thank you!

1 answer

6


You need to configure the property selectionBehavior from Listview to value SelectRows. If not, Listview spoils your selection.

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

list->setSelectionBehavior(QAbstractItemView::SelectRows);
  • Marcos, the system still doesn’t allow me to award the reward (only in 17 hours). Then I give it to you, ok? :)

  • 1

    Understood. I would just like to ask, so that in future questions, it was said which version of Qt was used. At first I thought it might be a bug in the Windows version. It would be difficult to test the problem on Windows, Linux with Qt4, Qt5.

  • True. If I had remembered earlier I would have edited the question and posed, but I think now is unnecessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.