Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to implement drag and drop feature in QLabel
Forum Updated to NodeBB v4.3 + New Features

How to implement drag and drop feature in QLabel

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    devillIsHeree
    wrote on last edited by
    #1

    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
    {
    Q_OBJECT
    public:
    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.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      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: https://doc.qt.io/qt-5/dnd.html
      So the label should implement the dragging part.
      The widget window should implement dropping part.

      ADDED
      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        devillIsHeree
        wrote on last edited by
        #3

        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 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4
          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.
          1 Reply Last reply
          0
          • D Offline
            D Offline
            devillIsHeree
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bonnie
              wrote on last edited by
              #6

              Sorry, I won't write code for you.

              1 Reply Last reply
              1
              • D Offline
                D Offline
                devillIsHeree
                wrote on last edited by
                #7

                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

                jsulmJ 1 Reply Last reply
                0
                • D devillIsHeree

                  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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @devillIsHeree If you need code then take a look at examples, for example: https://doc.qt.io/qt-5/qtwidgets-draganddrop-draggableicons-example.html

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • D Offline
                    D Offline
                    devillIsHeree
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved