// Create a label widget QLabel *label = new QLabel("Drag me", this); // Enable drag operation for the label label->setDragEnabled(true);
// Implement the drag and drop event handler for a custom widget void MyWidget::mousePressEvent(QMouseEvent *event) { // Start a drag operation QDrag *drag = new QDrag(this); // Set the MIME data for the drag operation QMimeData *mimeData = new QMimeData(); mimeData->setText("Hello World!"); drag->setMimeData(mimeData); // Start the drag operation drag->exec(); }This example shows how to implement the mousePressEvent() event handler for a custom widget. When the mouse button is pressed and held down on the widget, a drag operation is started using the QDrag class. The MIME data for the drag operation is set to a text string and the drag operation is started. Package library: Qt5Widgets.