Check if button was right clicked or left clicked?
-
You won't get a click event when anything else but the left button is clicked. If you want to handle other mouse buttons too you need to override QPushButton::mousePressEvent()
-
wrote on 21 Apr 2019, 08:42 last edited by
Can you give an example of how to override the QPushButton::mousePressEvent()? I don't know how to handle two different outcomes like this in one override!
Semi-relateable: do I have different options? I.e. can I override the clicked() signal or do I HAVE TO override the mousePressEvent in order to do this? Would it be messy if I created a new signal and connected a button twice?
-
You have to override the mousePressEvent() as I already said in my post above. And the link I gave you also shows you how to check for the correct mouse button.
-
Hi
For your use case, you could add new signal to
your derived QPushButton to allow it to emit
rightclicked() and connect to that new signal outside
to handle when its right-clicked.Note: its pretty uncommon to right click buttons in terms
of user experience. -
wrote on 21 Apr 2019, 08:55 last edited by
When I try to add the QPushButton mouseEventPress() function into my code and redefine it it gives this error
member access into incomplete type 'QAbstractButtonPrivate'
-
You should not add the code from QAbstractButton::mousePressEvent() - you should override the function. This is basic c++ knowledge explained e.g. here: https://www.programiz.com/cpp-programming/function-overriding
-
wrote on 21 Apr 2019, 09:22 last edited by
All I get when I try to override the function is
non-friend class member 'mouseEventPress' cannot have a qualified name
-
All I get when I try to override the function is
non-friend class member 'mouseEventPress' cannot have a qualified name
@legitnameyo
show the code :) -
wrote on 21 Apr 2019, 09:34 last edited by
class Derived: public QPushButton { public: QPushButton A; QAbstractButton B(Derived); QAbstractButton::mouseMoveEvent(QMouseEvent *e); // Error void QAbstractButton::mousePressEvent(QMouseEvent *e) // Error { Q_D(QAbstractButton); if (e->button() != Qt::LeftButton) { e->ignore(); return; } if (hitButton(e->pos())) { setDown(true); d->pressed = true; repaint(); d->emitPressed(); e->accept(); } else { e->ignore(); } } };
-
class Derived: public QPushButton { public: QPushButton A; QAbstractButton B(Derived); QAbstractButton::mouseMoveEvent(QMouseEvent *e); // Error void QAbstractButton::mousePressEvent(QMouseEvent *e) // Error { Q_D(QAbstractButton); if (e->button() != Qt::LeftButton) { e->ignore(); return; } if (hitButton(e->pos())) { setDown(true); d->pressed = true; repaint(); d->emitPressed(); e->accept(); } else { e->ignore(); } } };
wrote on 21 Apr 2019, 09:51 last edited by KillerSmath@legitnameyo
You cannot define a qualifier in declaration of a member method.class Derived : public QPushButton{ public: //void QPushButton::mousePressEvent(QMouseEvent *e); // wrong void mousePressEvent(QMouseEvent *e); // right };
Further, you should to define the construtor method of your class.
Note: The construtor method is a function without return value and with same name of class.class Derived : public QPushButton{ public: Derived(); // construtor }
To accept signals and slot feature, you need to declare the
Q_OBJECT
inside your class.class Derived : QPushButton{ Q_OBJECT ... };
Below is an example code of how you should reimplement it using @mrjj idea.
class CustomPushButton : public QPushButton { Q_OBJECT public: CustomPushButton(QWidget *parent = nullptr); protected: void mousePressEvent(QMouseEvent *e) override; void mouseReleaseEvent(QMouseEvent *e) override; signals: void leftClicked(); void rightClicked(); };
Now, you need to reimplement the mousePressedEvent and mouseReleaseEvent(if necessary) logic on your .cpp file
-
wrote on 21 Apr 2019, 11:52 last edited by
all I get from that class is
:-1: error: symbol(s) not found for architecture x86_64 :-1: error: linker command failed with exit code 1 (use -v to see invocation)
-
@legitnameyo said in Check if button was right clicked or left clicked?:
all I get from that class is
This is no class, this is a class definition. You have to fill out the functions by yourself with the code you want to have - we won't write you your code...
-
wrote on 21 Apr 2019, 12:02 last edited by
I can't write out the functions since everything I want to override or change in QPushButton::mousePressEvent is private.
-
wrote on 21 Apr 2019, 12:11 last edited by
Solved it by creating a derivative of the CustomQPushButton class definition! Thanks!
11/15