Skip to content

Commit

Permalink
Improve drag-and-drop visual handling in DragWidget
Browse files Browse the repository at this point in the history
Modified the mousePressEvent in DragWidget to
temporarily detach the dragged QLabel from its
parent using setParent(nullptr). This ensures the
dragged widget remains visible and above other
widgets during the drag operation. Re-parenting
logic was added to restore the QLabel to its o
riginal container if the drag action is not a
MoveAction. This change addresses visual issues
where dragged items could disappear behind
overlapping widgets, enhancing user experience
and maintaining consistent behavior.

Task-number: QTBUG-123777
Pick-to: 6.6.2
Change-Id: I3edce9c96815e32eb8f00b61f7eda1709de04b4d
Reviewed-by: Richard Moe Gustavsen <[email protected]>
  • Loading branch information
Pixelowski committed Dec 5, 2024
1 parent 2021d39 commit 8e3f04f
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions examples/widgets/draganddrop/draggableicons/dragwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,13 @@ void DragWidget::mousePressEvent(QMouseEvent *event)
drag->setHotSpot(event->position().toPoint() - child->pos());
//! [3]

QPixmap tempPixmap = pixmap;
QPainter painter;
painter.begin(&tempPixmap);
painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
painter.end();

child->setPixmap(tempPixmap);
child->setParent(nullptr);

if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
child->close();
delete child;
} else {
child->setParent(this);
child->move(event->position().toPoint() - drag->hotSpot());
child->show();
child->setPixmap(pixmap);
}
}

0 comments on commit 8e3f04f

Please sign in to comment.