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 get a pointer to a widget at current mouse position?

How to get a pointer to a widget at current mouse position?

Scheduled Pinned Locked Moved Unsolved General and Desktop
widgetqtabbardrag
14 Posts 2 Posters 13.0k 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.
  • U Offline
    U Offline
    Unknown-T
    wrote on last edited by Unknown-T
    #3

    @mrjj How can I use drag and drop for usual windows or widgets? I want to drag my tab as usual window (as if I dragged window title area). And how can I recieve the dragEnter event in this case?
    eventFilter is unsuitable, because I need to know to which object I should send the event.

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

      Subclass is the only way then. (if u cant use event filter)
      You can drag most things as a custom format.
      But not sure a window will trigger it if drag by caption as that is NOT a real drag.
      However, you might be able to make it work with startdrag.

      1 Reply Last reply
      1
      • U Offline
        U Offline
        Unknown-T
        wrote on last edited by
        #5

        @mrjj >But not sure a window will trigger it if drag by caption as that is NOT a real drag.
        That's okay. I can reimplement mouseMoveEvent of some widget and drag window by dragging the QTabBar widget(as you can see while dragging tabs in Chrome).
        What should I do to make DragEnterEvents, dropEvents, etc. triggered? How to start 'real' drag and recieve the dragEvents? I have tried to reimplement the dragEnterEvent, but it activates only if I drag some text/file inside it, but if I drag window it doesn't trigger.

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

          Hi
          problem is that dragging by caption triggers nchittest native event.
          So not sure how to do it with an actual window as we need to call startdrag as we would for a widget or
          any place from the client area of the window.
          this following code is for dragging an item from list to area. (using a user image)
          so you could render the window as smaller? version and use that for drop icon but
          it comes back to how to detect the caption drag.

          I have no good idea for that if to be crossplatform. maybe others have better/some ideas.

          void PiecesList::startDrag(Qt::DropActions /*supportedActions*/) {
            QListWidgetItem* item = currentItem();
          
            QByteArray itemData;
            QDataStream dataStream(&itemData, QIODevice::WriteOnly);
            QPixmap pixmap = qvariant_cast<QPixmap>(item->data(Qt::UserRole));
            QPoint location = item->data(Qt::UserRole + 1).toPoint();
            QString test;
            test="text";
            dataStream << pixmap << location << test;
          
            QMimeData* mimeData = new QMimeData;
            mimeData->setData("image/x-puzzle-piece", itemData);
          
            QDrag* drag = new QDrag(this);
            drag->setMimeData(mimeData);
            drag->setHotSpot(QPoint(pixmap.width() / 2, pixmap.height() / 2));
            drag->setPixmap(pixmap);
          
            if (drag->exec(Qt::MoveAction) == Qt::CopyAction)
              delete takeItem(row(item));
          }
          
          

          Code mostly from
          http://doc.qt.io/qt-5/qtwidgets-draganddrop-puzzle-example.html
          ps. Test code. sloppy..:)

          1 Reply Last reply
          0
          • U Offline
            U Offline
            Unknown-T
            wrote on last edited by
            #7

            @mrjj okay, let's imagine that we solved problem with caption drag detection.
            My problem is that i don't want to render small image of my tab, I'd like to drag real tab as real window. I saw many examples, where dragging labels/pictures is shown, and where data is converted into bytearray. But I drag widget, which contains another child widgets, such as textareas with text inside and other, so i can't convert them into bytearray... That's why I thought that Drag&Drop isn't good way for me.

            I want to implement algorythm nearly like this:

            1. If user dragged tab outside QTabBar -> tab became a window and follow the mouse cursor.
            2. If count of tabs in current TabBar is 1, when user drag tab he just moves the window, no new windows created.
            3. If user dragged one tab (window) to some TabBar -> window becomes tab. //That's the main problem.
              Initially, everything was perfect, i implemented the first two steps, just by using mouseMoveEvents & it's globalPos() method, without using D&D. But the third step is my problem for many days...
            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #8

              Hi
              I can only think of bytearray with widget pointers and
              drop target would re-parent them and close window.
              But again, i dont know how to catch title dragging to start a drag.

              I would check with event filter on the test drop target to see if any info is even sent
              to the drop target if u drag a window over it.

              1 Reply Last reply
              0
              • U Offline
                U Offline
                Unknown-T
                wrote on last edited by
                #9

                @mrjj Reparenting is not a problem too. Problem is how to know, that the mouse cursor is inside QTabBar, during dragging the tab (window). If we will know it, we can do everything we want.

                mrjjM 1 Reply Last reply
                0
                • U Unknown-T

                  @mrjj Reparenting is not a problem too. Problem is how to know, that the mouse cursor is inside QTabBar, during dragging the tab (window). If we will know it, we can do everything we want.

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

                  @Unknown-T
                  well
                  what about finding widgetAt and reuse source and make it able to ignore a widget ?

                  U 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @Unknown-T
                    well
                    what about finding widgetAt and reuse source and make it able to ignore a widget ?

                    U Offline
                    U Offline
                    Unknown-T
                    wrote on last edited by Unknown-T
                    #11

                    @mrjj
                    That's a great idea, but how can we make the widgetAt() able to ignore some widget? I thought about it earlier, but I don't know how to ignore a widget.

                    mrjjM 1 Reply Last reply
                    0
                    • U Unknown-T

                      @mrjj
                      That's a great idea, but how can we make the widgetAt() able to ignore some widget? I thought about it earlier, but I don't know how to ignore a widget.

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

                      @Unknown-T
                      Hi
                      We need to find the source code and see how it works before we can see
                      if possible to alter it for ignore a widget.

                      U 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @Unknown-T
                        Hi
                        We need to find the source code and see how it works before we can see
                        if possible to alter it for ignore a widget.

                        U Offline
                        U Offline
                        Unknown-T
                        wrote on last edited by
                        #13

                        @mrjj Hello.
                        I have found source code of the widgetAt() method, but, as i understood, It's based on the loop and is hard to modify.
                        So I decided to send some signal, while moving the window, to all avaible QTabBars and they will send a reply signal if their rect() contains current mouse position. That's the only way, that i see.

                        mrjjM 1 Reply Last reply
                        0
                        • U Unknown-T

                          @mrjj Hello.
                          I have found source code of the widgetAt() method, but, as i understood, It's based on the loop and is hard to modify.
                          So I decided to send some signal, while moving the window, to all avaible QTabBars and they will send a reply signal if their rect() contains current mouse position. That's the only way, that i see.

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

                          Hi
                          Sounds ok to me. another signal based version of widgetAt :)

                          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