[SOLVED] QTreeWidget: Drag and Drop.



  • Hi all,

    I have main window with QGraphicsScene subclass and QTreeWidget subclass. In QTreeWidget subclass I reimplemented mousePressEvent() and mouseMoveEvent() for adding ability drag and drop operation. In tree widget I add some checkable items and I can add to scene from tree widget graphic item using drag and drop or just set checkbox on the tree widget's item. Everything works fine but I have little bug. After item from tree widget was dropped into the scene, for selecting other item on tree widget I must click two times on this item (I can set checkbox but item still not selected and I must click on item again).
    My code for mousePressEvent() and mouseMoveEvent():

    void MyTreeWidget::mousePressEvent(QMouseEvent *event) {
        QTreeWidget::mousePressEvent(event);
    
        if (event->button() == Qt::LeftButton)
            _dragStartPos = event->pos();
    }
    
    void MyTreeWidget::mouseMoveEvent(QMouseEvent *event) {
        QTreeWidget::mouseMoveEvent(event);
    
        QTreeWidgetItem *item = itemAt(_dragStartPos);
        if (!item)
            return;
    
        if (item->checkState(0) == Qt::Checked)
            return;
    
        for (int i = 0; i < topLevelItemCount(); ++i) {
            if (topLevelItem(i) == item)
                return;
        }
    
        QByteArray itemData;
        itemData.setNum(item->data(0, Qt::UserRole).toInt());
    
        QMimeData *mimeData = new QMimeData;
        mimeData->setData("mydata", itemData);
    
        QDrag *drag = new QDrag(this);
        drag->setMimeData(mimeData);
        drag->exec(Qt::MoveAction);
    }
    

    Thanks for any help.


  • Lifetime Qt Champion

    Hi,

    What don't you use the drag and drop functionality already implemented in the item view classes ?





  • I tried to implement startDrag() method instead mouseMoveEvent() and this works fine for main window. But if I call second window (modal) from my main window the drag operation not working for second window.



  • Modal window supposed not to be able to interact with other widgets.
    It is the purpose of modality.
    You should change the design to modeless if you need such interaction.



  • @alex_malyu said:

    Modal window supposed not to be able to interact with other widgets.
    It is the purpose of modality.
    You should change the design to modeless if you need such interaction.

    Thanks, but in my case drag operation not working inside modal window when I implemented startDrag() method instead mouseMoveEvent().


  • Lifetime Qt Champion

    @Vovasty Yes, that's the documentation I was thinking about



  • @SGaist

    Ok, thank you.


Log in to reply
 

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