Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to make a QLabel follow a mouse pointer and display over other widgets?
Forum Updated to NodeBB v4.3 + New Features

How to make a QLabel follow a mouse pointer and display over other widgets?

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 4 Posters 4.5k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    MikeLemon
    wrote on last edited by
    #1

    I was wondering how do you draw over other widgets in PyQt?
    The intention is to make a binary indicator of X,Y cords with a pyQtGraph widget and I want to binarize the display of plotted dots on the graph to be displayed by the X Coordinated of the mouse and by a single or two labels to display the XY coordinated of the plotted graph near the mouse while following it.

    How would one approach it? (QToolTip option is out as it doesn't seem to respond well and isn't constantly ON)

    I would like to know in general the way of overlaying widgets on top of other in an absolute way with XY pixel coordinates.

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

      Hi,

      You can use mouse tracking and move a QLabel along the mouse pointer.

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

      M 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        You can use mouse tracking and move a QLabel along the mouse pointer.

        M Offline
        M Offline
        MikeLemon
        wrote on last edited by
        #3

        @SGaist Hi,

        How is it accepted to add and move the QLabel to the layout for that application?
        How'd you receive the Cursor position?

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

          In this case, you do not put the QLabel in a layout. You move it manually.

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

          M 1 Reply Last reply
          1
          • SGaistS SGaist

            In this case, you do not put the QLabel in a layout. You move it manually.

            M Offline
            M Offline
            MikeLemon
            wrote on last edited by
            #5

            @SGaist That's what I don't understand how to do I'm got my redefinition of QMainWindow and set a plot widget as the central widget for now but how'd you add a QLabel of top of it and position it according to the mouse cursor position is something I can't find a solution for.

            I'd be useful if an example of how'd you add a QLabel to the MainWindow and change it's position would be added here.

            jsulmJ J.HilkJ 2 Replies Last reply
            0
            • M MikeLemon

              @SGaist That's what I don't understand how to do I'm got my redefinition of QMainWindow and set a plot widget as the central widget for now but how'd you add a QLabel of top of it and position it according to the mouse cursor position is something I can't find a solution for.

              I'd be useful if an example of how'd you add a QLabel to the MainWindow and change it's position would be added here.

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

              @MikeLemon said in How to make a QLabel follow a mouse pointer and display over other widgets?:

              but how'd you add a QLabel of top of it

              You create the QLabel, set its parent to your central widget, but do not add it to any layout, call show() on it.
              "position it according to the mouse cursor position" - you get the mouse coordinates from the event, then call https://doc.qt.io/qt-5/qwidget.html#pos-prop on the label.

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

              1 Reply Last reply
              1
              • M MikeLemon

                @SGaist That's what I don't understand how to do I'm got my redefinition of QMainWindow and set a plot widget as the central widget for now but how'd you add a QLabel of top of it and position it according to the mouse cursor position is something I can't find a solution for.

                I'd be useful if an example of how'd you add a QLabel to the MainWindow and change it's position would be added here.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @MikeLemon

                #include <QMouseEvent>
                
                class MyMainWindow : public QMainWindow
                {
                    Q_OBJECT
                public:
                    explicit MyMainWindow (QWidget *parent = nullptr) : QMainWindow(parent) {
                        setMouseTracking(true);
                        m_myLabel = new QLabel(this);
                        m_myLabel->setStyleSheet(QString("background-color:red"));
                        m_myLabel->resize(160,40);
                
                    }
                
                protected:
                    void mouseMoveEvent(QMouseEvent *event) override
                    {
                        auto pos = event->pos();
                        m_myLabel->move(pos);
                        m_myLabel->setText(QString("Mouse at: (%1 | %2)").arg(pos.x()).arg(pos.y()));
                    }
                
                private:
                    QLabel *m_myLabel;
                };
                
                
                int main(int argc, char *argv[])
                {
                
                    QApplication a(argc, argv);
                
                    MyMainWindow m;
                    m.resize(400, 400);
                    m.show();
                    a.exec();
                }
                
                #include "main.moc"
                

                I'm sure you're able to adapt this example for your case.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                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