how to show coordinates of mouse in statusbar?



  • Just like the title. How can I show the real-time coordinates of the mouse in the statusbar like many drawing app?


  • Qt Champions 2016

    Hi
    You can use a timer and QCursor::pos().

    If you have a drawing area like a drawing app would, you can also
    override the mouseMoveEvent(QMouseEvent* event) for that widget.

    You can also use a EventFilter if multiple widgets are involved.

    It depends on how many widgets that should show info when mouse is over.
    If one big area, then mouseMoveEvent might be preferable.



  • Sorry,I've tried some methods to override the mouseMoveEvent(QMouseEvent*) for that widget and also setMouseTracking(true) in the construction function, it still doesn't work. Could you please tell me more details?



  • @mrjj

    setStatusTip (QString::number( event->pos().x() ) + ", " +QString::number( event->pos().y() ));
    

    I override mouseMoveEvent(QMouseEvent*) with this. but it just show the coordinates only when the mouse leave the statusbur for widget.



  • @Geng.Y Hi,friend.welcome.

    I use the simple demo to simulate your requirements.

    Just code snippet, not the all.

    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        /** Use the QLabel object to show info */
        _label = new QLabel(this);
        ui->statusBar->addWidget(_label);
    
        /** Note: if you used the QMainWindow, you must both setMouseTracking true in QMainWindow class and centralWidget */
        setMouseTracking(true);
        ui->centralWidget->setMouseTracking(true);
    }
    
    void MainWindow::mouseMoveEvent(QMouseEvent *event)
    {
        QString text;
        text = QString("%1 X %2").arg(event->pos().x()).arg(event->pos().y());
        /** Update the info text */
        _label->setText(text);
    }
    


  • Thank you very much!



  • @joeQ
    Does anyone know why something like QString::point() was not or if we can ask that it will be in the future?


  • Qt Champions 2016

    @ofmrew

    QString::point() ?

    What you mean by that ?



  • @joeQ
    I placed Widget and then propagated it to MyCanvas. MyCanvas::mouseMoveEvent(QMouseEvent *event has be overridden to perform another function and it does not set accept() ; however, it does not seem to proprogate the event up to mainWindow as no values are displayed when the mouse is over MyCanvas. I know that MyCanvas gets the mouse points. Any idea why the event is not propagated up?



  • @mrjj

    QString::number() converts a number into a string, why not have QString::point() convert a point into a string. I need to do that quire often. I found the post about using statusBar to display the value of the mouse location interesting because I want to do the same thing; however, it does not work because I promoted the UI Widget to my class. See my other post on that subject. Back to displaying the value of a point in the statusBar, I did that in 1990 with an object named also named statusBar, Namely, statusBar show: point asString, simple in Smalltalk.


  • Qt Champions 2016

    @ofmrew said in how to show coordinates of mouse in statusbar?:

    why the event is not propagated up?

    Do you call the base class mouseMoveEvent ?

    MyCanvas::mouseMoveEvent(QMouseEvent *event )  {
    ...
    baseclass::mouseMoveEvent(event);
    }
    

    (baseclass being the real name of your concrete base class for MyCanvas)

    • QString::point()
      Ah. well you can easy make your own function to accept a point and return some string.
      No reason to include directly in QString when different people might need different formatting
      on the returned string. One could argue why it cant take QRect etc. ( 2 points)


  • @mrjj
    Thanks, it worked like a charm; I will use it constantly.

    As to the point, my use for it is in debugging, so the same format used there is more than satisfactory. If I then want another format, then I will happily program that. But, the debugging format will satisfy 95% of my needs.

    This method solves the issue of mouse move events, but there is still the issue of adding text to the statusBar when the object is not communication with the mainwindow.

    Again thanks, one more issue solved.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.