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. QListWidget drag&drop item disappear

QListWidget drag&drop item disappear

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 2.2k Views
  • 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.
  • P Offline
    P Offline
    Paddle
    wrote on 24 Mar 2023, 07:53 last edited by Paddle
    #1

    Hi,
    I have a QListWidget. Each item has a widget containing the checkboxes Qlabels and button. (I know custom delegate would be better).
    I enabled drap and drop with:

    ui->wbList->setDragDropMode(QAbstractItemView::InternalMove);
    ui->wbList->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->wbList->viewport()->setAcceptDrops(true);
    ui->wbList->setDropIndicatorShown(true);
    ui->wbList->setDragEnabled(true);
    ui->wbList->setDefaultDropAction(Qt::MoveAction);
    

    It works but there's a bug : if I drop an item just below itself, then it disappear. Dropping just above works correctly. So it feels like when it's trying to replace itself, in one direction it looses the widget by doing so.

    Is there a way to fix that?
    ezgif.com-crop.gif

    Thanks !
    Edit: same as this unsolved.
    More information here. It is apparantly a bug in newer versions of Qt.

    P 1 Reply Last reply 24 Mar 2023, 10:26
    0
    • P Paddle
      24 Mar 2023, 07:53

      Hi,
      I have a QListWidget. Each item has a widget containing the checkboxes Qlabels and button. (I know custom delegate would be better).
      I enabled drap and drop with:

      ui->wbList->setDragDropMode(QAbstractItemView::InternalMove);
      ui->wbList->setSelectionMode(QAbstractItemView::SingleSelection);
      ui->wbList->viewport()->setAcceptDrops(true);
      ui->wbList->setDropIndicatorShown(true);
      ui->wbList->setDragEnabled(true);
      ui->wbList->setDefaultDropAction(Qt::MoveAction);
      

      It works but there's a bug : if I drop an item just below itself, then it disappear. Dropping just above works correctly. So it feels like when it's trying to replace itself, in one direction it looses the widget by doing so.

      Is there a way to fix that?
      ezgif.com-crop.gif

      Thanks !
      Edit: same as this unsolved.
      More information here. It is apparantly a bug in newer versions of Qt.

      P Offline
      P Offline
      Paddle
      wrote on 24 Mar 2023, 10:26 last edited by
      #2

      I found a way to prevent the bug from happening by reimplementing QListWidget and overriding dragMoveEvent :

      class QListWidgetDragBugFix : public QListWidget
      {
          Q_OBJECT
      
      public:
          QListWidgetDragBugFix(QWidget *parent);
          ~QListWidgetDragBugFix() override;
      
      protected:
          void dragMoveEvent(QDragMoveEvent *e) override;
      };
      
      
      QListWidgetDragBugFix::QListWidgetDragBugFix(QWidget * parent)
        : QListWidget(parent)
      {
      }
      
      QListWidgetDragBugFix::~QListWidgetDragBugFix()
      {
      }
      
      /* Qt has a recent bug (2023, https://bugreports.qt.io/browse/QTBUG-100128) 
      * where the items disappears in certain conditions when drag and dropping.
      * Here we prevent the situation where this happens.
      * 1 - If the item is dropped on the item below such that the item doesn't move (ie superior half of the below item)
      * 2 - The item is the last one and user drop it on the empty space below.
      * In both those cases the item widget was lost.
       */
      void QListWidgetCustom::dragMoveEvent(QDragMoveEvent *e)
      {
          if ((row(itemAt(e->pos())) == currentRow() + 1) 
              || (currentRow() == count() - 1 && row(itemAt(e->pos())) == -1)) {
              e->ignore();
          }
          else {
              QListWidget::dragMoveEvent(e);
          }
      }
      
      C 1 Reply Last reply 24 Mar 2023, 11:15
      1
      • P Paddle
        24 Mar 2023, 10:26

        I found a way to prevent the bug from happening by reimplementing QListWidget and overriding dragMoveEvent :

        class QListWidgetDragBugFix : public QListWidget
        {
            Q_OBJECT
        
        public:
            QListWidgetDragBugFix(QWidget *parent);
            ~QListWidgetDragBugFix() override;
        
        protected:
            void dragMoveEvent(QDragMoveEvent *e) override;
        };
        
        
        QListWidgetDragBugFix::QListWidgetDragBugFix(QWidget * parent)
          : QListWidget(parent)
        {
        }
        
        QListWidgetDragBugFix::~QListWidgetDragBugFix()
        {
        }
        
        /* Qt has a recent bug (2023, https://bugreports.qt.io/browse/QTBUG-100128) 
        * where the items disappears in certain conditions when drag and dropping.
        * Here we prevent the situation where this happens.
        * 1 - If the item is dropped on the item below such that the item doesn't move (ie superior half of the below item)
        * 2 - The item is the last one and user drop it on the empty space below.
        * In both those cases the item widget was lost.
         */
        void QListWidgetCustom::dragMoveEvent(QDragMoveEvent *e)
        {
            if ((row(itemAt(e->pos())) == currentRow() + 1) 
                || (currentRow() == count() - 1 && row(itemAt(e->pos())) == -1)) {
                e->ignore();
            }
            else {
                QListWidget::dragMoveEvent(e);
            }
        }
        
        C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 24 Mar 2023, 11:15 last edited by
        #3

        Please create bug report about this.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        J 1 Reply Last reply 24 Mar 2023, 11:26
        0
        • C Christian Ehrlicher
          24 Mar 2023, 11:15

          Please create bug report about this.

          J Offline
          J Offline
          JonB
          wrote on 24 Mar 2023, 11:26 last edited by
          #4

          @Christian-Ehrlicher
          That user has done so in an existing bug referenced from the SO links posted earlier, see https://bugreports.qt.io/browse/QTBUG-100128

          C 1 Reply Last reply 24 Mar 2023, 11:32
          0
          • J JonB
            24 Mar 2023, 11:26

            @Christian-Ehrlicher
            That user has done so in an existing bug referenced from the SO links posted earlier, see https://bugreports.qt.io/browse/QTBUG-100128

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 24 Mar 2023, 11:32 last edited by
            #5

            @JonB thx

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            C 1 Reply Last reply 24 Mar 2023, 12:56
            0
            • C Christian Ehrlicher
              24 Mar 2023, 11:32

              @JonB thx

              C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 24 Mar 2023, 12:56 last edited by
              #6

              As written in the bug report I can't reproduce it with Qt 5.15.2 (Linux) or Qt6.6 (Linux + Windows). We need a reproducible example.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              P 1 Reply Last reply 24 Mar 2023, 13:03
              0
              • C Christian Ehrlicher
                24 Mar 2023, 12:56

                As written in the bug report I can't reproduce it with Qt 5.15.2 (Linux) or Qt6.6 (Linux + Windows). We need a reproducible example.

                P Offline
                P Offline
                Paddle
                wrote on 24 Mar 2023, 13:03 last edited by
                #7

                I work on a very large project so I'm actually not sure how to provide a basic example.

                However in this description of the problem : https://stackoverflow.com/questions/74263946/widget-inside-qlistwidgetitem-disappears-after-internal-move

                And this one : https://stackoverflow.com/questions/72529707/qlistwidget-items-dissapear-when-drag-dropped

                It appears that you just need a 'QListWidget' which is populated by QLabel or QCheckbox via .setItemWidget() and a drag and drop mode InternalMove.

                Then you should have this behavior, widget disappearing.

                C 1 Reply Last reply 24 Mar 2023, 14:39
                0
                • P Paddle
                  24 Mar 2023, 13:03

                  I work on a very large project so I'm actually not sure how to provide a basic example.

                  However in this description of the problem : https://stackoverflow.com/questions/74263946/widget-inside-qlistwidgetitem-disappears-after-internal-move

                  And this one : https://stackoverflow.com/questions/72529707/qlistwidget-items-dissapear-when-drag-dropped

                  It appears that you just need a 'QListWidget' which is populated by QLabel or QCheckbox via .setItemWidget() and a drag and drop mode InternalMove.

                  Then you should have this behavior, widget disappearing.

                  C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 24 Mar 2023, 14:39 last edited by
                  #8

                  I won't write a proper testcase for you. My testcase is working so there is no bug until I see it in a minimal, compilable example.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  Axel SpoerlA 1 Reply Last reply 24 Mar 2023, 22:38
                  0
                  • C Christian Ehrlicher
                    24 Mar 2023, 14:39

                    I won't write a proper testcase for you. My testcase is working so there is no bug until I see it in a minimal, compilable example.

                    Axel SpoerlA Offline
                    Axel SpoerlA Offline
                    Axel Spoerl
                    Moderators
                    wrote on 24 Mar 2023, 22:38 last edited by Axel Spoerl
                    #9

                    @Christian-Ehrlicher
                    Oh, that's a complicated case, see https://bugreports.qt.io/browse/QTBUG-100128.
                    In a nut shell: When a QListWidgetItemis dragged and dropped, the information about it is rightfully stored in mime data and the item is reconstructed from that data at its new position. That works fine, as long as we're talking about a QListWidgetItem.

                    In that particular case, an item widget takes it over: It carries the information to be displayed on the screen and the list widget item becomes just a container.

                    When the drag and drop happens, it's not supported to serialize all information about an arbitrary widget into mime data and reconstruct it upon drop. That case can only be solved on application level. The data necessary to reconstruct an item widget could for instance be stored in a QVariant on list widget item level. The drop event would need to be intercepted and the item widget reconstructed.

                    It's not a bug.

                    Software Engineer
                    The Qt Company, Oslo

                    J 1 Reply Last reply 25 Mar 2023, 07:30
                    0
                    • Axel SpoerlA Axel Spoerl
                      24 Mar 2023, 22:38

                      @Christian-Ehrlicher
                      Oh, that's a complicated case, see https://bugreports.qt.io/browse/QTBUG-100128.
                      In a nut shell: When a QListWidgetItemis dragged and dropped, the information about it is rightfully stored in mime data and the item is reconstructed from that data at its new position. That works fine, as long as we're talking about a QListWidgetItem.

                      In that particular case, an item widget takes it over: It carries the information to be displayed on the screen and the list widget item becomes just a container.

                      When the drag and drop happens, it's not supported to serialize all information about an arbitrary widget into mime data and reconstruct it upon drop. That case can only be solved on application level. The data necessary to reconstruct an item widget could for instance be stored in a QVariant on list widget item level. The drop event would need to be intercepted and the item widget reconstructed.

                      It's not a bug.

                      J Offline
                      J Offline
                      JonB
                      wrote on 25 Mar 2023, 07:30 last edited by
                      #10

                      @Axel-Spoerl
                      Hi Axel. I'm afraid I don't think your explanation is right. Remember the issue is about a particular item, along the lines of the last one and where you drop it to. I don't think "mime data" is relevant here.

                      Have a look again at https://bugreports.qt.io/browse/QTBUG-100128. I think someone else may have posted there subsequent to your post.

                      Axel SpoerlA 1 Reply Last reply 25 Mar 2023, 08:13
                      0
                      • J JonB
                        25 Mar 2023, 07:30

                        @Axel-Spoerl
                        Hi Axel. I'm afraid I don't think your explanation is right. Remember the issue is about a particular item, along the lines of the last one and where you drop it to. I don't think "mime data" is relevant here.

                        Have a look again at https://bugreports.qt.io/browse/QTBUG-100128. I think someone else may have posted there subsequent to your post.

                        Axel SpoerlA Offline
                        Axel SpoerlA Offline
                        Axel Spoerl
                        Moderators
                        wrote on 25 Mar 2023, 08:13 last edited by
                        #11

                        @JonB
                        Well, thanks for the reminder, Jon. I will look into that again.
                        In my tests, the item was prepared to be dragged away, e.g. to another list widget. Loss of widget item is unavoidable in that case.

                        Software Engineer
                        The Qt Company, Oslo

                        J 1 Reply Last reply 25 Mar 2023, 08:47
                        0
                        • Axel SpoerlA Axel Spoerl
                          25 Mar 2023, 08:13

                          @JonB
                          Well, thanks for the reminder, Jon. I will look into that again.
                          In my tests, the item was prepared to be dragged away, e.g. to another list widget. Loss of widget item is unavoidable in that case.

                          J Offline
                          J Offline
                          JonB
                          wrote on 25 Mar 2023, 08:47 last edited by
                          #12

                          @Axel-Spoerl
                          The OP here wrote in first post:

                          It works but there's a bug : if I drop an item just below itself, then it disappear. Dropping just above works correctly. So it feels like when it's trying to replace itself, in one direction it looses the widget by doing so.

                          This is an (attempted) drag-drop to move one list item within the QListWidget it is already in to a different index within that list widget. For that there should be no serialization/deserialization. It appears there is a specific case --- I think to do with being or being dragged to the last item --- which goes wrong. The "fix" @Paddle has posted into https://bugreports.qt.io/browse/QTBUG-100128 for his QListWidgetDragBugFix introduces a special case to ignore where

                              if ((row(itemAt(e->pos())) == currentRow() + 1) 
                                  || (currentRow() == count() - 1 && row(itemAt(e->pos())) == -1))
                          

                          so it's to handle a very specific case.

                          Axel SpoerlA 1 Reply Last reply 25 Mar 2023, 09:36
                          0
                          • J JonB
                            25 Mar 2023, 08:47

                            @Axel-Spoerl
                            The OP here wrote in first post:

                            It works but there's a bug : if I drop an item just below itself, then it disappear. Dropping just above works correctly. So it feels like when it's trying to replace itself, in one direction it looses the widget by doing so.

                            This is an (attempted) drag-drop to move one list item within the QListWidget it is already in to a different index within that list widget. For that there should be no serialization/deserialization. It appears there is a specific case --- I think to do with being or being dragged to the last item --- which goes wrong. The "fix" @Paddle has posted into https://bugreports.qt.io/browse/QTBUG-100128 for his QListWidgetDragBugFix introduces a special case to ignore where

                                if ((row(itemAt(e->pos())) == currentRow() + 1) 
                                    || (currentRow() == count() - 1 && row(itemAt(e->pos())) == -1))
                            

                            so it's to handle a very specific case.

                            Axel SpoerlA Offline
                            Axel SpoerlA Offline
                            Axel Spoerl
                            Moderators
                            wrote on 25 Mar 2023, 09:36 last edited by
                            #13

                            So, it appears that my judgement was wrong. Thanks, @JonB for pointing it out.
                            I suggest to close this thread and follow the case up in https://bugreports.qt.io/browse/QTBUG-100128.
                            I'll reopen it. Please check there if the latest reproducer reproduces the problem correctly.
                            Over and out.

                            Software Engineer
                            The Qt Company, Oslo

                            C 1 Reply Last reply 31 Mar 2023, 11:23
                            1
                            • Axel SpoerlA Axel Spoerl
                              25 Mar 2023, 09:36

                              So, it appears that my judgement was wrong. Thanks, @JonB for pointing it out.
                              I suggest to close this thread and follow the case up in https://bugreports.qt.io/browse/QTBUG-100128.
                              I'll reopen it. Please check there if the latest reproducer reproduces the problem correctly.
                              Over and out.

                              C Online
                              C Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 31 Mar 2023, 11:23 last edited by
                              #14

                              Fixed in Qt6.6

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              Axel SpoerlA 1 Reply Last reply 31 Mar 2023, 18:24
                              2
                              • C Christian Ehrlicher
                                31 Mar 2023, 11:23

                                Fixed in Qt6.6

                                Axel SpoerlA Offline
                                Axel SpoerlA Offline
                                Axel Spoerl
                                Moderators
                                wrote on 31 Mar 2023, 18:24 last edited by
                                #15

                                @Christian-Ehrlicher ...and 6.5 and 6.2.

                                Software Engineer
                                The Qt Company, Oslo

                                C 1 Reply Last reply 31 Mar 2023, 18:42
                                0
                                • Axel SpoerlA Axel Spoerl
                                  31 Mar 2023, 18:24

                                  @Christian-Ehrlicher ...and 6.5 and 6.2.

                                  C Online
                                  C Online
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 31 Mar 2023, 18:42 last edited by
                                  #16

                                  @Axel-Spoerl Correct, but imo we should put it into 5.15 too but it's commercial only so no actions from my side possible.

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  Axel SpoerlA 1 Reply Last reply 31 Mar 2023, 19:05
                                  0
                                  • C Christian Ehrlicher
                                    31 Mar 2023, 18:42

                                    @Axel-Spoerl Correct, but imo we should put it into 5.15 too but it's commercial only so no actions from my side possible.

                                    Axel SpoerlA Offline
                                    Axel SpoerlA Offline
                                    Axel Spoerl
                                    Moderators
                                    wrote on 31 Mar 2023, 19:05 last edited by
                                    #17

                                    @Christian-Ehrlicher I've alrady cherry-picked it there, but integration fails with unrelated errors. Need to investigate...after the weekend.

                                    Software Engineer
                                    The Qt Company, Oslo

                                    1 Reply Last reply
                                    0
                                    • J JonB referenced this topic on 28 Sept 2023, 14:16

                                    1/17

                                    24 Mar 2023, 07:53

                                    • Login

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