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
QtWS25 Last Chance

How to get item after drag item from QListWidget Thumbnails

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 2 Posters 3.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.
  • A Offline
    A Offline
    amarism
    wrote on last edited by amarism
    #1

    I am trying to find the item number or position that is drag from QListWidgetItem Thumbnails.
    I want to call this function when i droping the item inside view

    void mainWindows::onThumbItemClicked(QListWidgetItem* item)
    {
    if (((obj->GetNumOfSeries()) > 0))
    {
        for (int i = 0; i < (obj->GetNumOfSeries()); i++)
        {
            if (this->ui->m_ThumblistWidget->item(i) == item)
              {
                obj->SeriesChange(i);
                LoadViewer(false);
              }
          }
       }
    }
    

    for this function calling I need to get ListWidgetItem when i select. This function is working when i will click the thumbnail item. Now, I want to call same function i will drag an item.

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

      This should be useful: https://forum.qt.io/post/444126 (QGraphicsSceneDragDropEvent and QDropEvent behave in the same way for this case)

      "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

        This should be useful: https://forum.qt.io/post/444126 (QGraphicsSceneDragDropEvent and QDropEvent behave in the same way for this case)

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

        @VRonin what is Module in this position " doSomethingWithModule(userRoleIter->value<Module>()); "

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

          That is very specific to the original question.
          What you really care about is the code above, up to if(!mimeReader.commitTransaction()) break;.
          At that point you have the row index inrow and the data contained in the cell you dragged from in modelData

          "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

            That is very specific to the original question.
            What you really care about is the code above, up to if(!mimeReader.commitTransaction()) break;.
            At that point you have the row index inrow and the data contained in the cell you dragged from in modelData

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

            @VRonin here modelData size is 4. and row is assign 2 in my code . I have write similar code for my problem.

            void QvtkOpenGLWidgetdrag::dropEvent(QDropEvent *event)
            {
            const QMimeData *mimeData = event->mimeData();
            QListWidgetItem *item = new QListWidgetItem;
            
            if (mimeData->hasFormat("application/x-qabstractitemmodeldatalist")) {
            	QByteArray encoded = mimeData->data("application/x-qabstractitemmodeldatalist");
            	QDataStream stream(&encoded, QIODevice::ReadOnly);
            
            	while (!stream.atEnd()) {
            		int row;
            		int col;
            		QMap<int, QVariant> roleDataMap;
            		stream >> row >> col >> roleDataMap;
            		QIcon icon = roleDataMap.value(1).value<QIcon>();
                            m_pixmap = icon.pixmap(icon.availableSizes().first());
            		//test->onThumbItemClicked(item);
            		update();
            	}
            }
            else {
            	event->ignore();
            }
            }
            
            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6
              • roleDataMap.value(1) 1 is Qt::DecorationRole, do you save the icon in that role in the model you are dragging from?
              • QListWidgetItem *item = new QListWidgetItem; is unused and leaked. What is that for?

              "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
                • roleDataMap.value(1) 1 is Qt::DecorationRole, do you save the icon in that role in the model you are dragging from?
                • QListWidgetItem *item = new QListWidgetItem; is unused and leaked. What is that for?
                A Offline
                A Offline
                amarism
                wrote on last edited by
                #7

                @VRonin just creating "QListWidgetItem *item = new QListWidgetItem;" to pass the item from this class

                VRoninV 1 Reply Last reply
                0
                • A amarism

                  @VRonin just creating "QListWidgetItem *item = new QListWidgetItem;" to pass the item from this class

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

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

                  just creating "QListWidgetItem *item = new QListWidgetItem;" to pass the item from this class

                  ???

                  "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

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

                    just creating "QListWidgetItem *item = new QListWidgetItem;" to pass the item from this class

                    ???

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

                    @VRonin When I start to drag item from list widget then who to call the same item directly inside the " onThumbItemClicked(QListWidgetItem* item) "

                    1 Reply Last reply
                    0
                    • 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

                                          • Login

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