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
QtWS25 Last Chance

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 24 Sept 2018, 10:28 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.

    J 1 Reply Last reply 24 Sept 2018, 11:06
    0
    • M ManlishPotato
      24 Sept 2018, 10:28

      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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 24 Sept 2018, 11:06 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 24 Sept 2018, 11:42
      2
      • J jsulm
        24 Sept 2018, 11:06

        @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 24 Sept 2018, 11:42 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?

        J 1 Reply Last reply 24 Sept 2018, 11:43
        0
        • M ManlishPotato
          24 Sept 2018, 11:42

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 24 Sept 2018, 11:43 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 24 Sept 2018, 11:56
          2
          • J jsulm
            24 Sept 2018, 11:43

            @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 24 Sept 2018, 11:56 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

            3/5

            24 Sept 2018, 11:42

            • Login

            • Login or register to search.
            3 out of 5
            • First post
              3/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved