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 show coordinates of mouse in statusbar?
Forum Updated to NodeBB v4.3 + New Features

how to show coordinates of mouse in statusbar?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 6.1k 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.
  • G Offline
    G Offline
    Geng.Y
    wrote on last edited by
    #3

    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?

    1 Reply Last reply
    0
    • mrjjM mrjj

      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.

      G Offline
      G Offline
      Geng.Y
      wrote on last edited by
      #4

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

      joeQJ 1 Reply Last reply
      0
      • G Geng.Y

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

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #5

        @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);
        }
        

        Just do it!

        O 2 Replies Last reply
        2
        • G Offline
          G Offline
          Geng.Y
          wrote on last edited by
          #6

          Thank you very much!

          1 Reply Last reply
          0
          • joeQJ joeQ

            @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);
            }
            
            O Offline
            O Offline
            ofmrew
            wrote on last edited by
            #7

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

            mrjjM 1 Reply Last reply
            0
            • O ofmrew

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

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

              @ofmrew

              QString::point() ?

              What you mean by that ?

              O 1 Reply Last reply
              0
              • joeQJ joeQ

                @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);
                }
                
                O Offline
                O Offline
                ofmrew
                wrote on last edited by
                #9

                @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?

                mrjjM 1 Reply Last reply
                0
                • mrjjM mrjj

                  @ofmrew

                  QString::point() ?

                  What you mean by that ?

                  O Offline
                  O Offline
                  ofmrew
                  wrote on last edited by
                  #10

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

                  1 Reply Last reply
                  0
                  • O ofmrew

                    @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?

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

                    @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)
                    O 1 Reply Last reply
                    2
                    • mrjjM mrjj

                      @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)
                      O Offline
                      O Offline
                      ofmrew
                      wrote on last edited by
                      #12

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

                      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