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 the pointer or name of a widget when dragging an item
Forum Updated to NodeBB v4.3 + New Features

How to get the pointer or name of a widget when dragging an item

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

    First, please watch the video to get the issue. Thank you.
    [youtube]https://www.youtube.com/watch?v=86TgqaUbCGE&ab_channel=Abon[/youtube]
    link text

    My Problem is the item is gone after dragging in a wrong widget.
    I've tried to use "enterEvent" or "dragEnterEvent" to get the pointer of the left widget in the video.
    But it can't, because it can't trigger the enterEvent or dragEnterEvent of the left widget when I'm dragging an item. Therefore, I can't stop the removing mechanism of the QListWidget.

    My current solution is to use "isQListWidget (bool)" as a condition to trigger the removing mechanism, but no way can make isQListWidget works.

    My code is:

    void listWidgetMaya::startDrag(Qt::DropActions supportedActions)
    {
        //Declare a QListWidgetItem and get the currentItem
        QList<QListWidgetItem*> items = this->selectedItems();
        foreach(QListWidgetItem *item, items){
            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);
            drag->setPixmap(QPixmap(":/icons/icon_app-16.png"));
            //Execute the moving
            drag->exec(Qt::MoveAction);
    
            //Removing Mechanism of QListWidget
            if( isQListWidget == true){
                this->cache_objectNames.removeAt(cache_objectNames.indexOf(item->text()));
                delete takeItem(row(item));
            }
        }
    
    
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi

      Have a look at

      https://doc.qt.io/qt-5/qtwidgets-itemviews-puzzle-example.html

      it checks if the drop was accepted. and then first delete the item.

      you code will just do , regardless if it was accepted or not.

      so you need like

      if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
              delete takeItem(row(item));
      
      SisqosS 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi

        Have a look at

        https://doc.qt.io/qt-5/qtwidgets-itemviews-puzzle-example.html

        it checks if the drop was accepted. and then first delete the item.

        you code will just do , regardless if it was accepted or not.

        so you need like

        if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
                delete takeItem(row(item));
        
        SisqosS Offline
        SisqosS Offline
        Sisqos
        wrote on last edited by
        #3

        @mrjj Thank you. The code works perfectly.

        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