Drag and Drop, MoveAction not deleting original position?



  • Hi,

    I have set 2 QListWidget that accept drag and drop,
    I'm moving one widget from list#1 to list#2, the item get copied but not moved, i'm trying to figure out why?
    Here is a screenshot of it :
    https://www.dropbox.com/s/ylm70suadtna9s3/DragDropInt.png

    Here is the current code :

    custom QListView

    @myQListWidget::myQListWidget(QWidget *parent) :QListWidget(parent)
    {
    setAcceptDrops(true);
    setDragDropMode(DragDrop);
    setDefaultDropAction(Qt::MoveAction);
    setDragEnabled(true);
    setDropIndicatorShown(true);
    }

    void myQListWidget::dragEnterEvent(QDragEnterEvent *event) {

    qDebug() << "dragEnterEvent";
    if (event->mimeData()->hasFormat("mt/interval") ) {
        qDebug() << "OK CAN ACCEPT DROP!";
        event->acceptProposedAction();
    }
    

    }
    void myQListWidget::dropEvent(QDropEvent *event) {

    qDebug() << "dropEvent";
    
    if (event->proposedAction() == Qt::MoveAction) {
    
        qDebug() << "EVENT SOURCE :" << event->source();
        /// Take QByteArray and create object from it
        //        const QMimeData *data = event->mimeData();
        //        QByteArray dataArray = data->data("mt/interval");
    
    
        QListWidgetItem* item;
        item = new QListWidgetItem(this);
        this->addItem(item);
    
        IntervalWidget *intervalWidget = new IntervalWidget();
        item->setSizeHint(intervalWidget->minimumSizeHint());
        this->setItemWidget(item, intervalWidget);
    
        event->acceptProposedAction();
    
        //        QListWidget::dropEvent(event);
    }
    

    }@

    Custom Widget

    @
    if (event->button() == Qt::LeftButton)
    dragStartPosition = event->pos();
    }

    void IntervalWidget::mouseMoveEvent(QMouseEvent *event) {

    if (!(event->buttons() & Qt::LeftButton))
        return;
    if ((event->pos() - dragStartPosition).manhattanLength()
            < QApplication::startDragDistance())
        return;
    
    QDrag *drag = new QDrag(this);
    QMimeData *mimeData = new QMimeData;
    
    QByteArray output;
    /// Write object to QByteArray
    mimeData->setData("mt/interval", output);
    drag->setMimeData(mimeData);
    

    // //// Image when moving the widget
    QPixmap pixmap(this->rect().size());
    this->render(&pixmap, QPoint(), QRegion(this->rect()));
    drag->setPixmap(pixmap);

    Qt::DropAction dropAction = drag->exec&#40;Qt::MoveAction&#41;;
    

    }@



  • edited


  • Lifetime Qt Champion

    Hi,

    Did you look at "this":http://qt-project.org/doc/qt-5/model-view-programming.html#using-convenience-views about the model view classes and d&d ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.