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 detect mouse button press
Forum Update on Monday, May 27th 2025

How to detect mouse button press

Scheduled Pinned Locked Moved Solved General and Desktop
c++qwidgetmouse eventnoob
5 Posts 2 Posters 22.1k Views
  • 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
    ManlishPotato
    wrote on last edited by
    #1

    Hi! I'm trying to detect if the right mouse button has been pressed, i'm doing this with this code from my mainwindow.cpp file:

    void MainWindow::mouseMoveEvent(QMouseEvent *e)
    {
        if(e->button() == Qt::RightButton)
        {
            qDebug() << "Right mouse click!" << endl;
        }
    }
    

    My problem is that i don't know how to call this function, because the function has "no name", what i mean is that the function is called mouseMoveEvent, but that is some kind of standard QWidget function and i haven't been able to call it. I think your supposed to use update() to call it in something like this:

    QTimer *timer = new QTimer(this);
     connect(timer, &QTimer::timeout, this, [this]{ update(); });
     timer->start(500);
    

    but this does nothing for me.

    I'm not entirely sure how to declare the function in .h file but this is how i've done it:

    public:
          void mouseMoveEvent(QMouseEvent *e);
    

    I think understanding this would help in a lot of other areas, so any help is appreciated, that been said keep in mind that i'm pretty new to qt, and have i limited understanding of c++...
    Thanks in advance! Benjamin.

    jsulmJ 1 Reply Last reply
    0
    • M ManlishPotato

      Hi! I'm trying to detect if the right mouse button has been pressed, i'm doing this with this code from my mainwindow.cpp file:

      void MainWindow::mouseMoveEvent(QMouseEvent *e)
      {
          if(e->button() == Qt::RightButton)
          {
              qDebug() << "Right mouse click!" << endl;
          }
      }
      

      My problem is that i don't know how to call this function, because the function has "no name", what i mean is that the function is called mouseMoveEvent, but that is some kind of standard QWidget function and i haven't been able to call it. I think your supposed to use update() to call it in something like this:

      QTimer *timer = new QTimer(this);
       connect(timer, &QTimer::timeout, this, [this]{ update(); });
       timer->start(500);
      

      but this does nothing for me.

      I'm not entirely sure how to declare the function in .h file but this is how i've done it:

      public:
            void mouseMoveEvent(QMouseEvent *e);
      

      I think understanding this would help in a lot of other areas, so any help is appreciated, that been said keep in mind that i'm pretty new to qt, and have i limited understanding of c++...
      Thanks in advance! Benjamin.

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

      @ManlishPotato Why do you override mouseMoveEvent if you want to detect mousePressEvent?
      http://doc.qt.io/qt-5/qwidget.html#mousePressEvent
      "My problem is that i don't know how to call this function" - you don't call it. It is done for you if the mouse is moved over your main window.
      You should read http://doc.qt.io/qt-5/eventsandfilters.html

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

      M 1 Reply Last reply
      2
      • jsulmJ jsulm

        @ManlishPotato Why do you override mouseMoveEvent if you want to detect mousePressEvent?
        http://doc.qt.io/qt-5/qwidget.html#mousePressEvent
        "My problem is that i don't know how to call this function" - you don't call it. It is done for you if the mouse is moved over your main window.
        You should read http://doc.qt.io/qt-5/eventsandfilters.html

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

        @jsulm Hi! Thanks for quick response!
        That totally worked! Thanks!
        Do you never call these types of functions or was it best to do it in this case?

        jsulmJ 1 Reply Last reply
        0
        • M ManlishPotato

          @jsulm Hi! Thanks for quick response!
          That totally worked! Thanks!
          Do you never call these types of functions or was it best to do it in this case?

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

          @ManlishPotato As I said you usually don't call them by yourself, they are called by Qt internals when an event arrives. And you should not call them in this case as well. If user presses a mouse button Qt will call http://doc.qt.io/qt-5/qwidget.html#mousePressEvent

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

          M 1 Reply Last reply
          2
          • jsulmJ jsulm

            @ManlishPotato As I said you usually don't call them by yourself, they are called by Qt internals when an event arrives. And you should not call them in this case as well. If user presses a mouse button Qt will call http://doc.qt.io/qt-5/qwidget.html#mousePressEvent

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

            @jsulm Ok! I will make sure to read all the links you've given me. Thanks for the help!

            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