Dragging a widget is not so simple. You need to do a lot of coding.
From Qt Docs :
void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton && iconLabel->geometry().contains(event->pos())) { QDrag *drag = new QDrag(this); QMimeData *mimeData = new QMimeData; mimeData->setText(commentEdit->toPlainText()); drag->setMimeData(mimeData); drag->setPixmap(iconPixmap); Qt::DropAction dropAction = drag->exec(); ... } }
This means that when your widget receives a message about dragging it, for example, like using the mouse, it should create a QDrag object.
In the QDrag object, you can set a pixmap representing your draggable widget. If you hide your widget at this moment, it looks as if your mouse pointer βtookβ the widget.
In addition, you need a QMimeData object. In this you can put all kinds of data that describe your widget. In your case, use something that allows you to identify your widget. Because, and here comes the difficult part: you must move yourself.
The widget that is the parent of the grid receives the drop event and reads from the mime data that the widget wishes me to move. From QDropEvent, it gets the point at which the widget should move. This is what you need to code: the actual move in the grid layout. And do not forget to update xml.
source share