Qt: show mouse position such as tooltip

As I write in the title, I hope to show the position of the mouse as a hint.
For this, I believe that I need to override QCursor, right?
But I do not know the details of QCursor and how to make a new cursorShape.
Is there such an example?

+4
source share
1 answer

Assuming that you want to read the coordinates when moving the cursor (as in many graphics or CAD applications), you really do not want to override QCursor .

The most efficient approach depends on which widget will provide the coordinates, but in most cases the easiest way would be to setMouseTracking(true) for the widget and override it with mouseMoveEvent(QMouseEvent* event) to display QToolTip as follows:

 void MyWidget::mouseMoveEvent(QMouseEvent* event) { QToolTip::showText(event->globalPos(), // In most scenarios you will have to change these for // the coordinate system you are working in. QString::number( event->pos().x() ) + ", " + QString::number( event->pos().y() ), this, rect() ); QWidget::mouseMoveEvent(event); // Or whatever the base class is. } 

Usually you will not β€œclick” such a tooltip; you should use QWidget::setToolTip(const QString&) or write hints in QWidget::event(QEvent*) . But a normal QToolTip only appears after a short delay, but you want them to be constantly updated.

I must say that I have not tried this, but so I did. Hope this helps!

+9
source

Source: https://habr.com/ru/post/1434113/


All Articles