Detecting what mouse-button is pressed on a icon-button (CPP)
-
I made a method that depended on two different buttons on a 'speed-bar' (bar with clickable-buttons)
//1. button QAction* strumNotesUpAction = new QAction(embed::getIconPixmap( "strumUp" ), tr( "Strum Selected by Q-value" ), this ); connect( strumNotesUpAction, SIGNAL( triggered() ), m_editor, SLOT( strumNotesUp() ) ); //2. button QAction* strumNotesDnAction = new QAction(embed::getIconPixmap( "strumDwn" ), tr( "Strum Selected by Q-value" ), this ); connect( strumNotesDnAction, SIGNAL( triggered() ), m_editor, SLOT( strumNotesDn() ) ); //add physical buttons for each of the defined QActions notesActionsToolBar->addAction( strumNotesUpAction ); notesActionsToolBar->addAction( strumNotesDnAction );
This works, but is not optimized.
I need to only use one button, and split the button-function between left-click and right-clickI searched in the examples and i found
template <typename Functor> QAction *QMenu::addAction( const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut = 0)
But that is for a key-board shortcut for the action. I need to catch the mouse-button (left/right) that is being pressed
Something like:
//button QAction* strumNotesAction = new QAction(embed::getIconPixmap( "strum" ), tr( "Strum Selected by Q-value" ), <MouseButtonPressed lft| rgt >, this);
Then i need to do a specified action depending on whitch mouse-button was pressed
if (???<left-mouse-signal>???) connect( strumNotesAction, SIGNAL( triggered() ), m_editor, SLOT( strumNotes( true) ) ) else connect( strumNotesAction, SIGNAL( triggered() ), m_editor, SLOT( strumNotes( false ) ) );
Where void strumNotes(bool bb) does the actual 'work' depending on mouse-button used.
I wonder iftriggered()
could be where the mouse-button is registered, but searching gives
About 7,350 results (0.23 seconds)
That is not possible to get an overview from..How can left and right-mouse button do two different actions through different parameters in the function-call?
-
Use QToolBar::addWidget() and pass your custom widget derived from QToolButton to it. In this class you can override the mousePressEvent for your needs.
-
Use QToolBar::addWidget() and pass your custom widget derived from QToolButton to it. In this class you can override the mousePressEvent for your needs.
@Christian-Ehrlicher Thanks Christian, but my toolbar is not a QToolBar, it just a row of buttons. I need to use that button, eg the type that is implemented as
QAction*
but that
QAction(embed::getIconPixmap(...)
has to react depending on weather left or right mouse-button is pressed.
It has to be a QAction!
All other neighbouring buttons are this kind.
The UI must remain consistent! -
I need to use that button, eg the type that is implemented as QAction*
A QAction is not a button, or a UI widget of any sort. It is an abstract representation of something that occurs when triggered. You either trigger it, or not.
A QAction in a QToolbar creates a button-per-action to trigger the action. In a QMenu it is a menu entry per action.
The non-standard UI you are describing is the equivalent of a single QToolButton with two associated QActions, one for up and one for down, and peculiar way of triggering them.
but my toolbar is not a QToolBar, it just a row of buttons
Excellent. You have total control of the widgets in your "tool" bar. So, do what @Christian-Ehrlicher suggested and insert a customised version of whatever widget the other buttons you are creating are. This presumably happens inside your notesActionsToolBar->addAction() function.
Here is an example:
#ifndef DUALACTIONPUSHBUTTON_H #define DUALACTIONPUSHBUTTON_H #include <QPushButton> class DualActionPushButton: public QPushButton { Q_OBJECT public: explicit DualActionPushButton(QWidget *p = nullptr); ~DualActionPushButton(); protected: void mousePressEvent(QMouseEvent *event); }; #endif // DUALACTIONPUSHBUTTON_H
#include <QMouseEvent> #include <QDebug> DualActionPushButton::DualActionPushButton(QWidget *p): QPushButton(p) { } DualActionPushButton::~DualActionPushButton() { } void DualActionPushButton::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { qDebug() << "Left"; // trigger the first action in the button's action list (if there is one). QList<QAction*> acts = this->actions(); if (acts.count() > 0) { acts.at(0)->trigger(); event->accept(); } } else if (event->button() == Qt::RightButton) { qDebug() << "Right"; // trigger the second action in the button's action list (if there is one) QList<QAction*> acts = this->actions(); if (acts.count() > 1) { acts.at(1)->trigger(); event->accept(); } } // Ignore other options event->ignore(); }
and the setup
QAction* strumNotesUpAction = new QAction(tr( "Strum Selected by Q-value" ), this ); connect( strumNotesUpAction, &QAction::triggered, [=]() { qDebug() << "strumUp triggered"; } ); //2. button QAction* strumNotesDnAction = new QAction(tr( "Strum Selected by Q-value" ), this ); connect( strumNotesDnAction, &QAction::triggered, [=]() { qDebug() << "strumDwn triggered"; } ); QPushButton *pushButton m= new DualActionPushButton(this); pushButton->addAction(strumNotesUpAction); pushButton->addAction(strumNotesDnAction); // insert into your tool bar
-
I need to use that button, eg the type that is implemented as QAction*
A QAction is not a button, or a UI widget of any sort. It is an abstract representation of something that occurs when triggered. You either trigger it, or not.
A QAction in a QToolbar creates a button-per-action to trigger the action. In a QMenu it is a menu entry per action.
The non-standard UI you are describing is the equivalent of a single QToolButton with two associated QActions, one for up and one for down, and peculiar way of triggering them.
but my toolbar is not a QToolBar, it just a row of buttons
Excellent. You have total control of the widgets in your "tool" bar. So, do what @Christian-Ehrlicher suggested and insert a customised version of whatever widget the other buttons you are creating are. This presumably happens inside your notesActionsToolBar->addAction() function.
Here is an example:
#ifndef DUALACTIONPUSHBUTTON_H #define DUALACTIONPUSHBUTTON_H #include <QPushButton> class DualActionPushButton: public QPushButton { Q_OBJECT public: explicit DualActionPushButton(QWidget *p = nullptr); ~DualActionPushButton(); protected: void mousePressEvent(QMouseEvent *event); }; #endif // DUALACTIONPUSHBUTTON_H
#include <QMouseEvent> #include <QDebug> DualActionPushButton::DualActionPushButton(QWidget *p): QPushButton(p) { } DualActionPushButton::~DualActionPushButton() { } void DualActionPushButton::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { qDebug() << "Left"; // trigger the first action in the button's action list (if there is one). QList<QAction*> acts = this->actions(); if (acts.count() > 0) { acts.at(0)->trigger(); event->accept(); } } else if (event->button() == Qt::RightButton) { qDebug() << "Right"; // trigger the second action in the button's action list (if there is one) QList<QAction*> acts = this->actions(); if (acts.count() > 1) { acts.at(1)->trigger(); event->accept(); } } // Ignore other options event->ignore(); }
and the setup
QAction* strumNotesUpAction = new QAction(tr( "Strum Selected by Q-value" ), this ); connect( strumNotesUpAction, &QAction::triggered, [=]() { qDebug() << "strumUp triggered"; } ); //2. button QAction* strumNotesDnAction = new QAction(tr( "Strum Selected by Q-value" ), this ); connect( strumNotesDnAction, &QAction::triggered, [=]() { qDebug() << "strumDwn triggered"; } ); QPushButton *pushButton m= new DualActionPushButton(this); pushButton->addAction(strumNotesUpAction); pushButton->addAction(strumNotesDnAction); // insert into your tool bar
-
@UIUser said in Detecting what mouse-button is pressed on a icon-button (CPP):
but to make an example!
Wouldn't this be a task of you? I mean - copy'n'pasting some code together is not something you should be proud of...
-
@UIUser said in Detecting what mouse-button is pressed on a icon-button (CPP):
but to make an example!
Wouldn't this be a task of you? I mean - copy'n'pasting some code together is not something you should be proud of...
-Everyone has to learn how to, before they can do.
Your opinion is noted, but good examples are often how creative thinking has to start.
That is my pow.
If i know, i share.