, :
class CRightClickEnabler : public QObject
{
public:
CRightClickEnabler(QAbstractButton * button): QObject(button), _button(button) {
button->installEventFilter(this);
};
protected:
inline bool eventFilter(QObject *watched, QEvent *event) override {
if (event->type() == QEvent::MouseButtonPress)
{
auto mouseEvent = (QMouseEvent*)event;
if (mouseEvent->button() == Qt::RightButton)
_button->click();
}
return false;
}
private:
QAbstractButton* _button;
};
:
connect(ui->pushButton, &QPushButton::clicked, [](){qDebug() << "Button clicked";});
new CRightClickEnabler(ui->pushButton);
From this moment, the signal clickedwill be triggered by clicking the right mouse button, as well as clicking the left mouse button. There is no need to delete this object - it uses it ui->pushButtonas the parent and Qt will be automatically deleted when the parent is destroyed.
Obviously, you can write two lines of code (literally) to announce a new signal here, and if you want to emit this signal when you right-click instead clicked.
source
share