Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Drag and drop in QTreeWidget. How to ?



  • I've got QTreeWidget base class with it's member @QTreeWidgetItem * m_draggedItem@
    I reimplement some of base methods:
    @
    void GroupTreeWidget::mousePressEvent(QMouseEvent * event) {

    m_draggedItem = itemAt(event->pos());
    
    if (m_draggedItem) {
    
        m_draggedItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled|Qt::ItemIsDragEnabled);
    
        QTreeWidgetItem * parent = m_draggedItem->parent();
    
        if (parent)
            parent->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled|Qt::ItemIsDragEnabled);        
    }    
    
    return QTreeWidget::mousePressEvent(event);
    

    }
    @
    @
    void GroupTreeWidget::mouseReleaseEvent(QMouseEvent * event) {

    prepareDragItem();
    return QTreeWidget::mouseReleaseEvent(event);
    

    }
    @
    @
    void GroupTreeWidget::dropEvent(QDropEvent * event) {

    if (m_draggedItem != NULL) {
    
        QTreeWidgetItem * destItem = itemAt(event->pos());
    
        if (destItem == NULL) { // some problem here need to revert drag event
            event->ignore();
            prepareDragItem();
            return;
        }
    
        bool parentIsFolder = destItem->data(0, PROP_FOLDER_ROLE).toBool();  
    
        if (event->dropAction() != Qt::MoveAction) {
            prepareDragItem();
            event->ignore();
            return;
        }
    
        if (parentIsFolder == false) {
            prepareDragItem();
            event->ignore();
            return;
        }
    
        int sourceID = m_draggedItem->data(0, PROP_ID_ROLE).toInt();
        int destID   = destItem->data(0, PROP_ID_ROLE).toInt();
    
        if (destID == sourceID) {
            prepareDragItem();
            event->ignore();
            return;
        }
    
        QSharedPointer<Group> child = GET_GROUP_MANAGER().getGroupById(sourceID);
    
        if (child.isNull()) {
            prepareDragItem();
            event->ignore();
            return; 
        }
    
        if (destID == child->getProperties().valueI(sxDB::groups::FLD_GROUP_PARENT_ID)) {
           prepareDragItem();
           event->ignore();
           return; 
        }            
    
        if (child->setParentId(destID) == false) {
            prepareDragItem();
            event->ignore();
            return;
        }
    }
    
    prepareDragItem();
    return QTreeWidget::dropEvent(event);
    

    }
    @
    And here is my prepareDragItem function:
    @void GroupTreeWidget::prepareDragItem() {

    if (m_draggedItem == NULL)
        return;
    
    if (m_draggedItem->data(0, PROP_FOLDER_ROLE).toBool())
        m_draggedItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled|Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled);
    else
        m_draggedItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled|Qt::ItemIsDragEnabled);
    
    QTreeWidgetItem * parent = m_draggedItem->parent();
    
    if (parent)
        parent->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled|Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled);
    
    m_draggedItem = NULL;    
    

    }@

    And here what i do in ctror:
    @setDragEnabled(true);
    setAcceptDrops(true);
    setDragDropMode(QAbstractItemView::InternalMove);@
    Now I have lot of bugs. Firs of all I need to insert only in folders(PROP_FOLDER_ROLE == true), I need to disable drop in item itself and it's parent. And I need to insert only in items not between them. And I need only move action inside this widget. Why do I do this ? - I need to change some data inside my program when I make a drop and block some drops.
    1)When I ignoring drop event my item deletes from it's current position - how to fix this ?
    2)When I drop Item between two items it's do the same (I need to get QTreeWidgetItem poiter based on some else not event pos()).


Log in to reply