An item in QListWidget is selected together with its neighbour after its icon was changed.



  • Hello everybody,

    I have a list widget class that inherits from QListWidget, and has a single selection mode:

    setSelectionMode(QAbstractItemView::SingleSelection);
    setSelectionBehavior(QAbstractItemView::SelectItems);
    

    It behaves as expected: when item is clicked on, it becomes "current" and "selected".

    The following sequence of steps causes this unexpected behaviour. Please see the links to the screenshots that illustrate each step.

    (1) Click on item 3
    (2) Modify item 3
    (3) Click on item 2
    (4) Click on item 3

    The step (2) is based on the user command unrelated to the widget. The following code is executed:

    void FramesWidget::updateIcon(const int idx)
    {
        QListWidgetItem * const it = item(idx);
        it->setIcon(new_icon);
    }
    

    The steps (1)-(3) look correct, at least on the surface. But in the next step when the item 3 is clicked on, both items 3 and 4 are selected. The same is true when item 4 is clicked on. The two items are now always selected together.

    Note that this happens only when the item is clicked on. When current item is changed with arrow keys, a single selection is made as expected.

    I tried to trace the QListWidget::itemSelectionChanged() signal, and found that it was emitted twice when one of the stuck together items is clicked on.

    It seems that something is corrupted when the new icon for the item 3 is set. Any suggestions how to fix this?

    Thank you in advance!

    Roni.

    Qt 5.7.0, 64 bit, Ubuntu 14.04


  • Lifetime Qt Champion

    Hi,

    Can you provide a minimal sample code that reproduce this behavior ?



  • Thank you for the reply, @ SGaist!

    It is possible to create an isolated test case, but it is not a quick project. The whole system is rather complex...

    I hoped that this was a known problem. The documentation mentioned that QListWidget was not meant to be a base class. Could that be the reason? Or perhaps its items were not meant to be editable?


  • Lifetime Qt Champion

    For known problems the best place to search is the bug report system.

    I have not heard of something similar to your current situation and without a way to reproduce it I won't be able to confirm only from the description.

    Nothing wrong with subclassing QListWidget however it's not the usual use case.

    No that's wrong, the items can be edited, if not then the model would have been read-only.



  • Thank you for the help, @SGaist.

    I will try to use QListView+QAbstractListModel, because eventually the list will need to have multiple selections, and more editing capabilities. If the issue does not go away, I will create an small test case, and report the bug.



  • Hi, did you reimplement any click event in FramesWidgett?
    when QListWidget::itemSelectionChanged() is triggered twice, could you check the address of the items and make sure they are different?

    could you try changing FramesWidget::updateIcon to:

    void FramesWidget::updateIcon(const int idx){
    model()->setData(model()->index(idx,0),new_icon,Qt::DecorationRole);
    }
    

    I will try to use QListView+QAbstractListModel

    No need to start from the interface, you can use QStandardItemModel to check if it works and change it later



  • Thank you, @VRonin!

    No, I did not reimplement any click events. I had a wrapper for QListWidget::setCurrentRow only. It was called when the new item was clicked or selected via spin box, and resulted in the QItemSelectionModel::selectionChanged being triggered. This was expected, and the argument (item 3) was correct. The second time QItemSelectionModel::selectionChanged was triggered by QAbstractItemView::mousePressEvent, (a) with the wrong argument (item 4) , and (b) resulting in multiple item selection when a single selection was expected.

    Thank you for the suggestions. I am afraid by now I am already deep in the process of replacing the code with QListView+QAbstractListModel. The code right now in a very intermediate state...

    I'd close the thread... I will post the update later and share whether the new approach solved the problem.



  • A quick update: I implemented the view and the model based on QListView and QAbstractItemModel respectively, and the issue went away.

    Thanks again @VRonin and @SGaist for your help and support!


Log in to reply
 

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