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. How to get item after drag item from QListWidget Thumbnails
Forum Update on Monday, May 27th 2025

How to get item after drag item from QListWidget Thumbnails

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 2 Posters 3.3k 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #10

    onThumbItemClicked

    Do you want to respond to a click or to a drag?

    "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

    A 1 Reply Last reply
    0
    • VRoninV VRonin

      onThumbItemClicked

      Do you want to respond to a click or to a drag?

      A Offline
      A Offline
      amarism
      wrote on last edited by
      #11

      @VRonin on drag bcz onClick is working fine for me.

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

        And do you want to know it as soon as the item is dragged or when it gets dropped somewhere else?

        "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

        A 1 Reply Last reply
        0
        • VRoninV VRonin

          And do you want to know it as soon as the item is dragged or when it gets dropped somewhere else?

          A Offline
          A Offline
          amarism
          wrote on last edited by
          #13

          @VRonin when item is drag from listWidget

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

            then you need to reimplement void startDrag(Qt::DropActions supportedActions) with:

            void startDrag(Qt::DropActions supportedActions){
            QTableWidget::startDrag(supportedActions);
            QModelIndexList indexes = selectionModel()->selectedIndexes();
                auto isNotDragEnabled = [this](const QModelIndex &index) {
                    return !(model()->flags(index) & Qt::ItemIsDragEnabled);
                };
                indexes.erase(std::remove_if(indexes.begin(), indexes.end(),
                                             isNotDragEnabled),
                              indexes.end());
            QVector<QListWidgetItem*> draggetItems;
            draggetItems.reserve(indexes.size());
            for(auto& index : qAsConst(indexes))
            draggetItems << itemAt(index.row()));
            // now draggetItems contains the list of dragged items
            

            "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

            A 2 Replies Last reply
            1
            • VRoninV VRonin

              then you need to reimplement void startDrag(Qt::DropActions supportedActions) with:

              void startDrag(Qt::DropActions supportedActions){
              QTableWidget::startDrag(supportedActions);
              QModelIndexList indexes = selectionModel()->selectedIndexes();
                  auto isNotDragEnabled = [this](const QModelIndex &index) {
                      return !(model()->flags(index) & Qt::ItemIsDragEnabled);
                  };
                  indexes.erase(std::remove_if(indexes.begin(), indexes.end(),
                                               isNotDragEnabled),
                                indexes.end());
              QVector<QListWidgetItem*> draggetItems;
              draggetItems.reserve(indexes.size());
              for(auto& index : qAsConst(indexes))
              draggetItems << itemAt(index.row()));
              // now draggetItems contains the list of dragged items
              
              A Offline
              A Offline
              amarism
              wrote on last edited by amarism
              #15

              @VRonin said in How to get item after drag item from QListWidget Thumbnails:

              QModelIndexList indexes = selectedIndexes();

              Its showing selectIndex() is undefined.

              I m added this one code for startDrag:-

              void GLWidgetdrag::startDrag(Qt::DropActions supportedActions)
              {
              QListWidgetItem* item = currentItem();
              QMimeData* mimeData = new QMimeData;
              QByteArray ba;
              ba = item->text().toLatin1().data();
              mimeData->setData("application/x-item", ba);
              QDrag* drag = new QDrag(this);
              drag->setMimeData(mimeData);
              if (drag->exec(Qt::MoveAction) == Qt::MoveAction) {
              	/*delete takeItem(row(item));
              	emit itemDroped();*/
              }
              }
              

              Still Here showing this one error " QListWidgetItem* item = currentItem(); "

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

                Its showing selectIndex() is undefined.

                yes, sorry, I forgot 1 call, it's selectionModel()->selectedIndexes(), corrected now. I have no idea what you are trying to do in your implementation

                "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

                A 1 Reply Last reply
                0
                • VRoninV VRonin

                  Its showing selectIndex() is undefined.

                  yes, sorry, I forgot 1 call, it's selectionModel()->selectedIndexes(), corrected now. I have no idea what you are trying to do in your implementation

                  A Offline
                  A Offline
                  amarism
                  wrote on last edited by
                  #17

                  @VRonin Again its showing error:

                  0_1541424882403_xfbf.png

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

                    That method should go in a subclass of QListWidget

                    "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

                    A 1 Reply Last reply
                    2
                    • VRoninV VRonin

                      That method should go in a subclass of QListWidget

                      A Offline
                      A Offline
                      amarism
                      wrote on last edited by
                      #19
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • VRoninV VRonin

                        then you need to reimplement void startDrag(Qt::DropActions supportedActions) with:

                        void startDrag(Qt::DropActions supportedActions){
                        QTableWidget::startDrag(supportedActions);
                        QModelIndexList indexes = selectionModel()->selectedIndexes();
                            auto isNotDragEnabled = [this](const QModelIndex &index) {
                                return !(model()->flags(index) & Qt::ItemIsDragEnabled);
                            };
                            indexes.erase(std::remove_if(indexes.begin(), indexes.end(),
                                                         isNotDragEnabled),
                                          indexes.end());
                        QVector<QListWidgetItem*> draggetItems;
                        draggetItems.reserve(indexes.size());
                        for(auto& index : qAsConst(indexes))
                        draggetItems << itemAt(index.row()));
                        // now draggetItems contains the list of dragged items
                        
                        A Offline
                        A Offline
                        amarism
                        wrote on last edited by
                        #20

                        @VRonin said in How to get item after drag item from QListWidget Thumbnails:

                        auto isNotDragEnabled = [this](const QModelIndex &index) {
                        return !isIndexDragEnabled(index);
                        };

                        showing error in this line " !isIndexDragEnabled(index) "

                        VRoninV 1 Reply Last reply
                        0
                        • A amarism

                          @VRonin said in How to get item after drag item from QListWidget Thumbnails:

                          auto isNotDragEnabled = [this](const QModelIndex &index) {
                          return !isIndexDragEnabled(index);
                          };

                          showing error in this line " !isIndexDragEnabled(index) "

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #21

                          Yep, sorry, corrected now

                          "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

                          A 1 Reply Last reply
                          0
                          • VRoninV VRonin

                            Yep, sorry, corrected now

                            A Offline
                            A Offline
                            amarism
                            wrote on last edited by amarism
                            #22

                            @VRonin Now i m getting this one error "ErrorC3867 'QModelIndex::row': non-standard syntax; use '&' to create a pointer to member" In this line " draggetItems << itemAt(index.row)); "

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

                              These are just typos that are easy to fix... c'mon. I just forgot (). index.row should be index.row()

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

                              • Login

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