Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. An item in QListWidget is selected together with its neighbour after its icon was changed.

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 1.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roni219
    wrote on last edited by roni219
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roni219
        wrote on last edited by roni219
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            roni219
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • R Offline
                R Offline
                roni219
                wrote on last edited by
                #7

                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.

                R 1 Reply Last reply
                0
                • R roni219

                  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.

                  R Offline
                  R Offline
                  roni219
                  wrote on last edited by roni219
                  #8

                  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!

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved