Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Detecting what mouse-button is pressed on a icon-button (CPP)
Forum Updated to NodeBB v4.3 + New Features

Detecting what mouse-button is pressed on a icon-button (CPP)

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 632 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    UIUser
    wrote on last edited by
    #1

    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-click

    I 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 if

    triggered()
    

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Use QToolBar::addWidget() and pass your custom widget derived from QToolButton to it. In this class you can override the mousePressEvent for your needs.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      U 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Use QToolBar::addWidget() and pass your custom widget derived from QToolButton to it. In this class you can override the mousePressEvent for your needs.

        U Offline
        U Offline
        UIUser
        wrote on last edited by
        #3

        @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!

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by ChrisW67
          #4

          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
          
          U 1 Reply Last reply
          6
          • C ChrisW67

            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
            
            U Offline
            U Offline
            UIUser
            wrote on last edited by
            #5

            @ChrisW67 I cant thank you enough!
            So kind of you to not only explain what i have to do, but to make an example!
            That is SO kind!
            Thank you so much. Really Appreciated!

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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...

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              U 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @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...

                U Offline
                U Offline
                UIUser
                wrote on last edited by
                #7

                @Christian-Ehrlicher

                -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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved