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. Drag and drop text displayed between QGLWidget
Forum Update on Monday, May 27th 2025

Drag and drop text displayed between QGLWidget

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 2.1k 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.
  • Q Offline
    Q Offline
    QtVik
    wrote on last edited by QtVik
    #1

    How to drag and drop text displayed in one QGLWidget to another QGLWidget within a QT application?

    I have re-implemented the methods dragMoveEvent, dropEvent, dragEnterEvent, dragLeaveEvent

    and included setAcceptDrops(true) in the constructor also.

    But still breakpoint is not hit in any of the above overridden methods even if i drag and drop between widgets.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you show the code you are using ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        QtVik
        wrote on last edited by
        #3

        @SGaist :
        Following is the code:

        class MyView: public QGLWidget, public baseWidget
        {
        public:
        MyView::MyView()
        {
        setAcceptDrops(true);
        }
        protected:
        dragMoveEvent(QDragMoveEvent* event)
        {
        event->accept();
        }
        dragEnterEvent(QDragEnterEvent* event)
        {
        event->acceptProposedAction();
        }
        dragLeaveEvent(QDragLeaveEvent* event)
        {
        }
        dropEvent(QDropEvent* event)
        {
        }
        };

        I am creating the views through mainWindow class derived from QMainWindow.
        Please advice me how to write a dragEvent() while dragging within two different views.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          These are empty methods, you won't get custom drag and drop behaviour with just that.

          Did you read the dedicated chapter about it in Qt's documentation ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Q 1 Reply Last reply
          1
          • SGaistS SGaist

            These are empty methods, you won't get custom drag and drop behaviour with just that.

            Did you read the dedicated chapter about it in Qt's documentation ?

            Q Offline
            Q Offline
            QtVik
            wrote on last edited by
            #5

            @SGaist Thank you.
            Will go through that and try..

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              The
              http://doc.qt.io/qt-5/qtwidgets-draganddrop-draggabletext-example.html
              is pretty close to what you want.

              Q 1 Reply Last reply
              1
              • mrjjM mrjj

                Hi
                The
                http://doc.qt.io/qt-5/qtwidgets-draganddrop-draggabletext-example.html
                is pretty close to what you want.

                Q Offline
                Q Offline
                QtVik
                wrote on last edited by
                #7

                @mrjj Thank you.
                Will try it and update.

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  QtVik
                  wrote on last edited by
                  #8

                  @SGaist @mrjj :

                  I was trying to use Q_DECLARE_METATYPE in drag object to copy the data from one widget to another.
                  The meta type is declared something like below:
                  Class MyView : public QGLWidget, public BaseWidget
                  {
                  public:
                  struct my_struct{
                  int m_var;
                  };
                  Q_DECLARE_METATYPE(my_struct)
                  };
                  But I am getting following errors:
                  error: explicit specialization in non-namespace scope ‘class MyView’
                  error: expected primary-expression before ‘>’ token reinterpret_cast< TYPE *>(quintptr(-1)));
                  error: expected ‘(’ before ‘my_struct’ Q_DECLARE_METATYPE(my_struct)

                  What is right way of declaring a Q_DECLARE_METATYPE ?

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi

                    I think it likes better if classes are not nested.

                    struct my_struct{
                    int m_var;
                    };
                    Q_DECLARE_METATYPE(my_struct)

                    Class MyView : public QGLWidget, public BaseWidget
                    {
                    public:
                    my_struct myvar;
                    };

                    1 Reply Last reply
                    1
                    • Q Offline
                      Q Offline
                      QtVik
                      wrote on last edited by QtVik
                      #10

                      @mrjj : Thank you! you are absolutely right :)

                      One more query is how to handle mouse events when drag object is used ?
                      drag object seems to block all the mouse events.

                      I have created a drag object inside mousePressEvent() . Wanted to copy the string list from one widget to another.
                      I wanted to handle dropping of QList in mouseReleaseEvent() but as soon as drag object is created it blocks all mouse events.

                      mrjjM 1 Reply Last reply
                      0
                      • Q QtVik

                        @mrjj : Thank you! you are absolutely right :)

                        One more query is how to handle mouse events when drag object is used ?
                        drag object seems to block all the mouse events.

                        I have created a drag object inside mousePressEvent() . Wanted to copy the string list from one widget to another.
                        I wanted to handle dropping of QList in mouseReleaseEvent() but as soon as drag object is created it blocks all mouse events.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #11

                        @QtVik
                        Super.

                        Regarding drag & drop.
                        You should fully use the drag system to do so.
                        http://doc.qt.io/qt-5/dnd.html
                        http://doc.qt.io/qt-5/qdropevent.html

                        Here it drops pictures.
                        http://doc.qt.io/qt-5/qtwidgets-draganddrop-puzzle-example.html
                        you can drop anything u like this way.

                        Q 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          @QtVik
                          Super.

                          Regarding drag & drop.
                          You should fully use the drag system to do so.
                          http://doc.qt.io/qt-5/dnd.html
                          http://doc.qt.io/qt-5/qdropevent.html

                          Here it drops pictures.
                          http://doc.qt.io/qt-5/qtwidgets-draganddrop-puzzle-example.html
                          you can drop anything u like this way.

                          Q Offline
                          Q Offline
                          QtVik
                          wrote on last edited by
                          #12

                          @mrjj Thank you ! will try that and update..

                          1 Reply Last reply
                          0
                          • Q Offline
                            Q Offline
                            QtVik
                            wrote on last edited by
                            #13

                            Hi @mrjj @SGaist : Thank you for the inputs. It worked !

                            1 Reply Last reply
                            1

                            • Login

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