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 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
    0
    • P Paddle

      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 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);
          }
      }
      
      Christian EhrlicherC 1 Reply Last reply
      1
      • P Paddle

        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);
            }
        }
        
        Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          Please create bug report about this.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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

          Christian EhrlicherC 1 Reply Last reply
          0
          • JonBJ JonB

            @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

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

            Christian EhrlicherC 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @JonB thx

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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
              0
              • Christian EhrlicherC Christian Ehrlicher

                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 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.

                Christian EhrlicherC 1 Reply Last reply
                0
                • P Paddle

                  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.

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    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 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

                    JonBJ 1 Reply Last reply
                    0
                    • Axel SpoerlA Axel Spoerl

                      @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.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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
                      0
                      • JonBJ JonB

                        @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 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

                        JonBJ 1 Reply Last reply
                        0
                        • Axel SpoerlA Axel Spoerl

                          @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.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on 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
                          0
                          • JonBJ JonB

                            @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 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

                            Christian EhrlicherC 1 Reply Last reply
                            1
                            • Axel SpoerlA Axel Spoerl

                              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.

                              Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 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
                              2
                              • Christian EhrlicherC Christian Ehrlicher

                                Fixed in Qt6.6

                                Axel SpoerlA Offline
                                Axel SpoerlA Offline
                                Axel Spoerl
                                Moderators
                                wrote on last edited by
                                #15

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

                                Software Engineer
                                The Qt Company, Oslo

                                Christian EhrlicherC 1 Reply Last reply
                                0
                                • Axel SpoerlA Axel Spoerl

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

                                  Christian EhrlicherC Online
                                  Christian EhrlicherC Online
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 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
                                  0
                                  • Christian EhrlicherC Christian Ehrlicher

                                    @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 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
                                    • JonBJ JonB referenced this topic on

                                    • Login

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