Pushbutton crashed.
-
Button.h
#ifndef LR_CLICKBUTTON_H #define LR_CLICKBUTTON_H #include <QPushButton> class LR_ClickButton: public QPushButton { Q_OBJECT public: LR_ClickButton(QWidget *parent = nullptr); protected: void mousePressEvent(QMouseEvent *e) override; void mouseReleaseEvent(QMouseEvent *e) override; signals: // Not needed, since one signal which sends MouseEvent to get handled in slot //void leftClicked(); //void rightClicked(); void myClicked(QMouseEvent *e); }; #endif // LR_CLICKBUTTON_H
Button.cpp
#include "lr_clickbutton.h" #include <QMouseEvent> #include <QDebug> LR_ClickButton::LR_ClickButton(QWidget *parent): QPushButton(parent) { } void LR_ClickButton::mousePressEvent(QMouseEvent *e) { QPushButton::mousePressEvent(e); } void LR_ClickButton::mouseReleaseEvent(QMouseEvent *e) { if(e->button() == Qt::LeftButton) { qDebug() << "Left"; emit myClicked(e); } else if (e->button() == Qt::RightButton) { qDebug() << "Right"; emit myClicked(e); } QPushButton::mouseReleaseEvent(e); }
MainWindow.h
public slots: void myButtonClicked(QMouseEvent *e);
MainWindow.cpp
// ######### C'TOR ##################### LR_ClickButton *button1 = new LR_ClickButton(this); centralWidget()->layout()->addWidget(button1); connect(button1, &LR_ClickButton::myClicked, this, &MainWindow::myButtonClicked); // ###################################### void MainWindow::myButtonClicked(QMouseEvent *e) { if(e->button() == Qt::LeftButton) { // Of course you can put anything you want here int val = ui->spinBox->value(); ui->spinBox->setValue(val + 1); } else if (e->button() == Qt::RightButton) { // or here int val = ui->spinBox->value(); ui->spinBox->setValue(val - 1); } }
Wasn't that hard, right? :)
Result:
3 left-clicks increase value by 3.
2 right-clicks decrease value by 2 again.
-
@Pablo-J-Rogina
Oh, I see. You may be right. Or it just might turn out the user was going to use a left-/-right-click on a button to inc/dec a number, you never know... :)On a different tone, it's "unusual" to have the user right-click on a button. Possible, but possibly not intuitive. A "rocker" pushbutton (click at left/right for different behaviour, but with left mouse) might be another possibility.
@JonB said in Pushbutton crashed.:
On a different tone, it's "unusual" to have the user right-click on a button. Possible, but possibly not intuitive
Might not be the most intuitive solution but this is actually used a lot in games (with a hint to this behavior ofc)
-
@JonB said in Pushbutton crashed.:
On a different tone, it's "unusual" to have the user right-click on a button. Possible, but possibly not intuitive
Might not be the most intuitive solution but this is actually used a lot in games (with a hint to this behavior ofc)
-
@Christian-Ehrlicher @Pl45m4 @JonB @Pablo-J-Rogina
Thank you very much, you don't know how it helped me. Otherwise, I would have to create an internal storage system for each button between the tabs and remove it when editing the line.
Yes, and a tool I am creating to help the 1 game community. How does it activate as resolved?
-
@Christian-Ehrlicher @Pl45m4 @JonB @Pablo-J-Rogina
Thank you very much, you don't know how it helped me. Otherwise, I would have to create an internal storage system for each button between the tabs and remove it when editing the line.
Yes, and a tool I am creating to help the 1 game community. How does it activate as resolved?
-
@Gutks said in Pushbutton crashed.:
How does it activate as resolved
Either at bottom of page Topic Tools button, or if you feel a particular post answered it the vertical
...
button at the right on that post.This is exactly what I was expecting and what I thought of.... These type of games, where you have ingame shops and a lot of inventories... If you left-click an item, you probably buy it from shop or use it, if you right-click, you drop it or sell it :)