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. Check if button was right clicked or left clicked?

Check if button was right clicked or left clicked?

Scheduled Pinned Locked Moved Solved General and Desktop
right-clickbuttonqmouseevent
15 Posts 4 Posters 7.1k 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.
  • L Offline
    L Offline
    legitnameyo
    wrote on last edited by
    #1

    I create and customize a new button when my "create button" button is pressed like this

    void MainWindow::on_btn_add_btn_clicked() {
            QPushButton *btn = new QPushButton(ui->frame_btn_list);
            connect(btn, SIGNAL(clicked()), SLOT(on_btn_clicked()));
    
            btn->setText(QString("New Button " + QString::number(total_buttons)));
            btn->move(0, 30*total_buttons);
            btn->resize(186, 30);
            btn->setStyleSheet(QString("redundant styling... ignore this"));
            btn->show();
    
            total_buttons += 1;
    }
    

    Simply but, what I need is a way to execute a function when one of the buttons that I've created with this function gets right clicked. I already have a way to check if it has been clicked (left clicked, by default) as you can see but now I need to know how to check if a button has been right clicked. Further I also need to know WHAT button has been right clicked, not just that a button has been right clicked! I solved this "what button has been clicked" issue with the default left click by using

         QPushButton * theButton = qobject_cast<QPushButton *> ( sender() );
    

    in the on_btn_clicked() function and then modifying and getting info from "theButton" in different ways since "theButton" is the same as the button that was pressed.

    I've seen a few ways online to do this but none connect and check if a SPECIFIC button (no other object such as a frame) has been right clicked.

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

      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()

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

      1 Reply Last reply
      4
      • L Offline
        L Offline
        legitnameyo
        wrote on last edited by
        #3

        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?

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

          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.

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

          1 Reply Last reply
          3
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            4
            • L Offline
              L Offline
              legitnameyo
              wrote on last edited by
              #6

              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'
              
              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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

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

                1 Reply Last reply
                3
                • L Offline
                  L Offline
                  legitnameyo
                  wrote on last edited by
                  #8

                  All I get when I try to override the function is

                  non-friend class member 'mouseEventPress' cannot have a qualified name
                  
                  mrjjM 1 Reply Last reply
                  0
                  • L legitnameyo

                    All I get when I try to override the function is

                    non-friend class member 'mouseEventPress' cannot have a qualified name
                    
                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @legitnameyo
                    show the code :)

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      legitnameyo
                      wrote on last edited by
                      #10
                      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();
                              }
                          }
                      };
                      
                      
                      KillerSmathK 1 Reply Last reply
                      0
                      • L legitnameyo
                        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();
                                }
                            }
                        };
                        
                        
                        KillerSmathK Offline
                        KillerSmathK Offline
                        KillerSmath
                        wrote on last edited by KillerSmath
                        #11

                        @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

                        @Computer Science Student - Brazil
                        Web Developer and Researcher
                        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                        1 Reply Last reply
                        5
                        • L Offline
                          L Offline
                          legitnameyo
                          wrote on last edited by
                          #12

                          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)
                          
                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

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

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

                            1 Reply Last reply
                            2
                            • L Offline
                              L Offline
                              legitnameyo
                              wrote on last edited by
                              #14

                              I can't write out the functions since everything I want to override or change in QPushButton::mousePressEvent is private.

                              1 Reply Last reply
                              0
                              • L Offline
                                L Offline
                                legitnameyo
                                wrote on last edited by
                                #15

                                Solved it by creating a derivative of the CustomQPushButton class definition! Thanks!

                                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