How to detect mouse button press
-
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. -
@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 -
@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