Important: Please read the Qt Code of Conduct -

How to implement drag and drop feature in QLabel

  • I have one class which is inheriting from QLabel and I want to implement drag and drop feature in this label so that user can easily move this label in my widget window, So far what I have developed is the body of that label which is My

    class dragLabel: public QLabel
    dragLabel(QWidget *parent=nullptr): QLabel(parent){}

    void dragEnterEvent(QDragEnterEvent *event);
    void dragLeaveEvent(QDragLeaveEvent *event);
    void dragMoveEvent(QDragMoveEvent *event);
    void dropEvent(QDropEvent *event);
    void mousePressEvent(QMouseEvent *ev);


    Now I don't know what code to fill inside them so that the user can drag and drop this label in my widget window.

  • So, you want to drag a label to another widget?
    Then the label class does not need to implement those drag*Event, but mouse*Event.
    Refer to this doc:
    So the label should implement the dragging part.
    The widget window should implement dropping part.

    If you just want to move a widget (change its position) inside its parent widget, there is no need to use drag and drop.
    Just implement those mouse*Event and move it.

  • Actually I have a widget window and a label inside it i want to move my label in that window using drag and drop because i am using mouse click to make the label hide by overriding the mousePressEvent, I want to implement the drag and drop feature so that i can move that label in the same window.
    can you please give me the full code because i already read these documentation but i am still not able to perform that I am not getting how to initialize mimedata, hotspot etc, Can you please provide me the simple code to move the label inside the window using drag and drop, it's very important, Thanks in advance.

    1. As I said, that moving situation doesn't suit drag and drop, only mousePress+mouseMove+mouseRelease+move.
    2. As I said, drag and drop also need to implement mousePressEvent. It's not possible if you think you can do that without overriding mousePressEvent.

  • ok please you just provide me drag and drop code to move a label in the widget window overriding mousePressEvent and other events. i just need that please provide me that

  • Sorry, I won't write code for you.

  • i am just saying if you know how to code to move the label using drag and drop so provide, I am not telling you to make my whole application, if you know how to do so please help that's why we are here if you don't know just say I don't know how to do that. Simple

  • Lifetime Qt Champion

    @devillIsHeree If you need code then take a look at examples, for example:

  • Actually I already see those examples but they are very complicated I am not able to get how to implement this drag and drop, HOw it is using pixmap and mimedata etc, I have seen all the examples and search all over the internet, Now i post the question please help me here don't give any reference. Please just give me the code how to move this QLabel in the Widget window using drag and drop.

Log in to reply