Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qt : show mouse position like tooltip

As I write on title, I hope to show mouse position like tooltip. To do that, I think that I have to override QCursor, right?

But I don't know the details of QCursor and how to make a new cursorShape.

Is there any example like this?

like image 716
Hyun-geun Kim Avatar asked Sep 14 '12 03:09

Hyun-geun Kim


People also ask

How do I get the cursor position in Qt?

To set or get the position of the mouse cursor use the static methods QCursor::pos() and QCursor::setPos().

How to use tooltip in QT?

The simplest and most common way to set a widget's tool tip is by calling its QWidget::setToolTip() function (static tool tips). Then the tool tip is shown whenever the cursor points at the widget.


1 Answers

Assuming you want a coordinate readout as you move the cursor (like in many graphics or CAD applications), you really do not want to override QCursor.

The most efficient approach depends on what widget will be providing the coordinates, but in most cases the simplest way will be to setMouseTracking(true) for the widget, and override it's mouseMoveEvent(QMouseEvent* event) to display a QToolTip like so:

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

Normally you would not 'force' a tooltip like this; you would use QWidget::setToolTip(const QString&) or capture tooltip events in QWidget::event(QEvent*). But normal QToolTips only appear after short delay, but you want them updated continuously.

I should state that I haven't tried this, but this is the way I would do it. Hope that helps!

like image 100
cmannett85 Avatar answered Oct 12 '22 16:10

cmannett85