QDrag *drag = new QDrag(this); drag->setMimeData(mimeData); // set the drag data drag->setPixmap(pixmap); // set the pixmap for the drag display drag->setHotSpot(rect().center()); // set the drag hot spot to the center of the current widget drag->exec(Qt::CopyAction); // start the drag operation with the Copy action
void MyWidget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { QDrag *drag = new QDrag(this); drag->setMimeData(mimeData); // set the drag data drag->setPixmap(pixmap); // set the pixmap for the drag display drag->setHotSpot(event->pos()); // set the drag hot spot to the mouse position drag->exec(Qt::CopyAction); // start the drag operation with the Copy action } }In this example, the mousePressEvent function is used to handle left button presses. When a press occurs, a new QDrag object is created and its mime data and pixmap are set. The setHotSpot function is used to set the hot spot to the current mouse position, which is specified using the event->pos() function. Finally, the drag operation is started with the Copy action. Package/library: This code uses the Qt framework.