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. Right or left mouse press event with new signal and slot method.
Forum Updated to NodeBB v4.3 + New Features

Right or left mouse press event with new signal and slot method.

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 3.2k Views 2 Watching
  • 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.
  • E Offline
    E Offline
    ekato993
    wrote on last edited by
    #1

    Qt new signal and slot method: link

    I am using the following connection method. Button is a QPushButton *button = new QPushButton

    connect(button, &QPushButton::released, this,[=] {
         MainWindow::click(i, j);
    });
    

    Now pushbutton connect with the left click. I would like to connect right click as well, but I would like to connect it with other function.

    JonBJ Pl45m4P 2 Replies Last reply
    0
    • E Offline
      E Offline
      ekato993
      wrote on last edited by
      #10

      Oh okey, I understand:

      myqpushbutton.h

      #pragma once
      
      #include <QPushButton>
      #include <QMouseEvent>
      
      class MyQPushButton : public QPushButton {
      
          Q_OBJECT
      
      public:
          explicit MyQPushButton(QWidget *parent = nullptr);
      
      protected:
         void	mousePressEvent(QMouseEvent *e) override;
         void mouseReleaseEvent(QMouseEvent *e) override;
      
      signals:
          void leftClick();
          void rightClick();
      
      };
      

      myqpushbutton.cpp

      #include "myqpushbutton.h"
      #include <QDebug>
      
      MyQPushButton::MyQPushButton(QWidget *parent):
          QPushButton(parent) {}
      
      void MyQPushButton::mousePressEvent(QMouseEvent *e)
      {
          QPushButton::mousePressEvent(e);
      }
      
      void MyQPushButton::mouseReleaseEvent(QMouseEvent *e)
      {
          if(e->button() == Qt::LeftButton)
          {
              qDebug() << "Left";
              emit leftClick();
          }
          else if (e->button() == Qt::RightButton) {
      
              qDebug() << "Right";
              emit rightClick();
      
          }
          QPushButton::mouseReleaseEvent(e);
      }
      

      connection

              connect(button, &MyQPushButton::leftClick, this,
                      [=]{
                          MainWindow::left(i, j);
                      });
              connect(button, &MyQPushButton::rightClick, this,
                      [=]{
                          MainWindow::right(i, j);
                      });
      
      Pl45m4P 1 Reply Last reply
      0
      • E ekato993

        Qt new signal and slot method: link

        I am using the following connection method. Button is a QPushButton *button = new QPushButton

        connect(button, &QPushButton::released, this,[=] {
             MainWindow::click(i, j);
        });
        

        Now pushbutton connect with the left click. I would like to connect right click as well, but I would like to connect it with other function.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #2

        @ekato993
        Right-clicks usually raise a QContextMenuEvent. Do you have a different usage? And for that matter one usually reacts on pushbuttons being clicked rather than released.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          ekato993
          wrote on last edited by ekato993
          #3

          I would like to use right and left mouse to different stuffs.

          connect(button, ???, this,[=] {
               MainWindow::rightClick(i, j);
          });
          
          artwawA 1 Reply Last reply
          0
          • E ekato993

            I would like to use right and left mouse to different stuffs.

            connect(button, ???, this,[=] {
                 MainWindow::rightClick(i, j);
            });
            
            artwawA Offline
            artwawA Offline
            artwaw
            wrote on last edited by
            #4

            @ekato993 Please read the post above and (I assume this is the widget you ask about) the documentation of QWidget, QAbstractButton and QPushButton. Once you that you'll notice that for left and right click (which is pointed out in the post above) corresponding signals are clicked() and customContextMenuRequested() - the latter assuming you've setContextMenuPolicy() accordingly).
            Cleaner way would be to write QPushButton's descendant and override mouse event, I think. Both options would work though.

            For more information please re-read.

            Kind Regards,
            Artur

            1 Reply Last reply
            0
            • E ekato993

              Qt new signal and slot method: link

              I am using the following connection method. Button is a QPushButton *button = new QPushButton

              connect(button, &QPushButton::released, this,[=] {
                   MainWindow::click(i, j);
              });
              

              Now pushbutton connect with the left click. I would like to connect right click as well, but I would like to connect it with other function.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #5

              @ekato993

              There was a similar issue / question before.

              Go through this topic.
              https://forum.qt.io/topic/118098/pushbutton-crashed

              I guess it helps.
              (especially my answer:
              https://forum.qt.io/topic/118098/pushbutton-crashed/21)

              (PushButton subclass, handle mouseEvent and emit custom "button left/right-clicked" signal)


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              2
              • E Offline
                E Offline
                ekato993
                wrote on last edited by
                #6

                I have created a MyQPushButton class

                MyQPushButton.h file

                #pragma once
                
                #include <QPushButton>
                #include <QMouseEvent>
                
                
                class MyQPushButton : public QPushButton {
                
                    Q_OBJECT
                
                public:
                    explicit MyQPushButton(QWidget *parent = 0);
                
                private slots:
                    void rightMousePressEvent(QMouseEvent *e);
                    void leftMousePressEvent(QMouseEvent *e);
                
                signals:
                    void rightClicked();
                    void leftClicked();
                
                };
                

                MyQPushButton.cpp file

                #include "myqpushbutton.h"
                
                MyQPushButton::MyQPushButton(QWidget *parent) :
                    QPushButton(parent) {}
                
                void MyQPushButton::rightMousePressEvent(QMouseEvent *e) {
                    if(e->button() == Qt::RightButton)
                        emit rightClicked();
                }
                
                void MyQPushButton::leftMousePressEvent(QMouseEvent *e) {
                    if(e->button() == Qt::LeftButton)
                        emit leftClicked();
                }
                
                

                mainwindow.cpp file

                MyQPushButton *button = new MyQPushButton;
                 connect(button, &MyQPushButton::leftClicked, this,
                      [=]{MainWindow::leftClickCell(i, j);});
                 connect(button, &MyQPushButton::rightClicked, this,
                      [=]{MainWindow::rightClickCell(i, j);});
                

                I am using qDebug() in the rightClickCell and leftClickCell function. The left clicking is an option for me, but I do not get any outputs, right click is disabled for me.

                Pl45m4P 1 Reply Last reply
                0
                • E ekato993

                  I have created a MyQPushButton class

                  MyQPushButton.h file

                  #pragma once
                  
                  #include <QPushButton>
                  #include <QMouseEvent>
                  
                  
                  class MyQPushButton : public QPushButton {
                  
                      Q_OBJECT
                  
                  public:
                      explicit MyQPushButton(QWidget *parent = 0);
                  
                  private slots:
                      void rightMousePressEvent(QMouseEvent *e);
                      void leftMousePressEvent(QMouseEvent *e);
                  
                  signals:
                      void rightClicked();
                      void leftClicked();
                  
                  };
                  

                  MyQPushButton.cpp file

                  #include "myqpushbutton.h"
                  
                  MyQPushButton::MyQPushButton(QWidget *parent) :
                      QPushButton(parent) {}
                  
                  void MyQPushButton::rightMousePressEvent(QMouseEvent *e) {
                      if(e->button() == Qt::RightButton)
                          emit rightClicked();
                  }
                  
                  void MyQPushButton::leftMousePressEvent(QMouseEvent *e) {
                      if(e->button() == Qt::LeftButton)
                          emit leftClicked();
                  }
                  
                  

                  mainwindow.cpp file

                  MyQPushButton *button = new MyQPushButton;
                   connect(button, &MyQPushButton::leftClicked, this,
                        [=]{MainWindow::leftClickCell(i, j);});
                   connect(button, &MyQPushButton::rightClicked, this,
                        [=]{MainWindow::rightClickCell(i, j);});
                  

                  I am using qDebug() in the rightClickCell and leftClickCell function. The left clicking is an option for me, but I do not get any outputs, right click is disabled for me.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #7

                  @ekato993

                  You did the wrong thing. Compare my code with your try.
                  You use a signal and its slot to emit your signal. I used the mouse(Press/Release)Event.


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply
                  2
                  • E Offline
                    E Offline
                    ekato993
                    wrote on last edited by
                    #8

                    I have maded the MyQPushButton class.

                    mainwindow.h important part:

                    public:
                         void createGrid();
                    
                    public slots:
                         void myButtonClicked(QMouseEvent *e, int row, int col);
                    

                    mainwindow.cpp important part:

                    QVector<QVector<QPushButton*>> buttons;
                    
                    void MainWindow::createGrid() {
                    	QFrame *frame = new QFrame(this);
                    	QGridLayout *layout = new QGridLayout(frame);
                    	buttons.resize(4);
                    	for(int i = 0; i < 4; ++i){
                    		buttons[i].resize(4);
                    		for(int j = 0; j < 4; ++j){
                    		MyQPushButton *button = new MyQPushButton;
                    		QMouseEvent *e = nullptr;
                    		connect(button, &MyQPushButton::myClicked, this,
                    			[=]{
                    				MainWindow::myButtonClicked(e,i, j);
                    			});
                    		connect(button, &MyQPushButton::myClicked, this,
                    			[=]{
                    				MainWindow::myButtonClicked(e, i, j);
                    			});
                    		layout->addWidget(button,i,j);
                    		buttons[i][j] = button;
                    		}
                    	}
                    	setCentralWidget(frame);
                    }
                    
                    void MainWindow::myButtonClicked(QMouseEvent *e, int row, int col)
                    {
                        if(e->button() == Qt::LeftButton)
                        {
                            qDebug() << "left" << row << col;
                            buttons[row][col].first->setStyleSheet("background-color: grey; color: red; font-size: 24px");
                    
                        }
                        else if (e->button() == Qt::RightButton) {
                    
                            qDebug() << "right" << row << col;
                            buttons[row][col].first->setStyleSheet("background-color: orange; color: red; font-size: 24px");
                    
                        }
                    }
                    

                    QMouseEvent *e = nullptr cause me crash. I do not know how to use QMouseEvent to connection.

                    Pl45m4P 1 Reply Last reply
                    0
                    • E ekato993

                      I have maded the MyQPushButton class.

                      mainwindow.h important part:

                      public:
                           void createGrid();
                      
                      public slots:
                           void myButtonClicked(QMouseEvent *e, int row, int col);
                      

                      mainwindow.cpp important part:

                      QVector<QVector<QPushButton*>> buttons;
                      
                      void MainWindow::createGrid() {
                      	QFrame *frame = new QFrame(this);
                      	QGridLayout *layout = new QGridLayout(frame);
                      	buttons.resize(4);
                      	for(int i = 0; i < 4; ++i){
                      		buttons[i].resize(4);
                      		for(int j = 0; j < 4; ++j){
                      		MyQPushButton *button = new MyQPushButton;
                      		QMouseEvent *e = nullptr;
                      		connect(button, &MyQPushButton::myClicked, this,
                      			[=]{
                      				MainWindow::myButtonClicked(e,i, j);
                      			});
                      		connect(button, &MyQPushButton::myClicked, this,
                      			[=]{
                      				MainWindow::myButtonClicked(e, i, j);
                      			});
                      		layout->addWidget(button,i,j);
                      		buttons[i][j] = button;
                      		}
                      	}
                      	setCentralWidget(frame);
                      }
                      
                      void MainWindow::myButtonClicked(QMouseEvent *e, int row, int col)
                      {
                          if(e->button() == Qt::LeftButton)
                          {
                              qDebug() << "left" << row << col;
                              buttons[row][col].first->setStyleSheet("background-color: grey; color: red; font-size: 24px");
                      
                          }
                          else if (e->button() == Qt::RightButton) {
                      
                              qDebug() << "right" << row << col;
                              buttons[row][col].first->setStyleSheet("background-color: orange; color: red; font-size: 24px");
                      
                          }
                      }
                      

                      QMouseEvent *e = nullptr cause me crash. I do not know how to use QMouseEvent to connection.

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by
                      #9

                      @ekato993 said in Right or left mouse press event with new signal and slot method.:

                      QMouseEvent *e = nullptr cause me crash.

                      Why do you create your own QMouseEvent? Just override the mousePressEvent from you QPushButton class and emit your own signal there. Then you can split it into right-clicked and left-clicked and connect your lambda to these signals.


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      0
                      • E Offline
                        E Offline
                        ekato993
                        wrote on last edited by
                        #10

                        Oh okey, I understand:

                        myqpushbutton.h

                        #pragma once
                        
                        #include <QPushButton>
                        #include <QMouseEvent>
                        
                        class MyQPushButton : public QPushButton {
                        
                            Q_OBJECT
                        
                        public:
                            explicit MyQPushButton(QWidget *parent = nullptr);
                        
                        protected:
                           void	mousePressEvent(QMouseEvent *e) override;
                           void mouseReleaseEvent(QMouseEvent *e) override;
                        
                        signals:
                            void leftClick();
                            void rightClick();
                        
                        };
                        

                        myqpushbutton.cpp

                        #include "myqpushbutton.h"
                        #include <QDebug>
                        
                        MyQPushButton::MyQPushButton(QWidget *parent):
                            QPushButton(parent) {}
                        
                        void MyQPushButton::mousePressEvent(QMouseEvent *e)
                        {
                            QPushButton::mousePressEvent(e);
                        }
                        
                        void MyQPushButton::mouseReleaseEvent(QMouseEvent *e)
                        {
                            if(e->button() == Qt::LeftButton)
                            {
                                qDebug() << "Left";
                                emit leftClick();
                            }
                            else if (e->button() == Qt::RightButton) {
                        
                                qDebug() << "Right";
                                emit rightClick();
                        
                            }
                            QPushButton::mouseReleaseEvent(e);
                        }
                        

                        connection

                                connect(button, &MyQPushButton::leftClick, this,
                                        [=]{
                                            MainWindow::left(i, j);
                                        });
                                connect(button, &MyQPushButton::rightClick, this,
                                        [=]{
                                            MainWindow::right(i, j);
                                        });
                        
                        Pl45m4P 1 Reply Last reply
                        0
                        • E ekato993

                          Oh okey, I understand:

                          myqpushbutton.h

                          #pragma once
                          
                          #include <QPushButton>
                          #include <QMouseEvent>
                          
                          class MyQPushButton : public QPushButton {
                          
                              Q_OBJECT
                          
                          public:
                              explicit MyQPushButton(QWidget *parent = nullptr);
                          
                          protected:
                             void	mousePressEvent(QMouseEvent *e) override;
                             void mouseReleaseEvent(QMouseEvent *e) override;
                          
                          signals:
                              void leftClick();
                              void rightClick();
                          
                          };
                          

                          myqpushbutton.cpp

                          #include "myqpushbutton.h"
                          #include <QDebug>
                          
                          MyQPushButton::MyQPushButton(QWidget *parent):
                              QPushButton(parent) {}
                          
                          void MyQPushButton::mousePressEvent(QMouseEvent *e)
                          {
                              QPushButton::mousePressEvent(e);
                          }
                          
                          void MyQPushButton::mouseReleaseEvent(QMouseEvent *e)
                          {
                              if(e->button() == Qt::LeftButton)
                              {
                                  qDebug() << "Left";
                                  emit leftClick();
                              }
                              else if (e->button() == Qt::RightButton) {
                          
                                  qDebug() << "Right";
                                  emit rightClick();
                          
                              }
                              QPushButton::mouseReleaseEvent(e);
                          }
                          

                          connection

                                  connect(button, &MyQPushButton::leftClick, this,
                                          [=]{
                                              MainWindow::left(i, j);
                                          });
                                  connect(button, &MyQPushButton::rightClick, this,
                                          [=]{
                                              MainWindow::right(i, j);
                                          });
                          
                          Pl45m4P Offline
                          Pl45m4P Offline
                          Pl45m4
                          wrote on last edited by
                          #11

                          @ekato993

                          That should work now. Does it?


                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                          ~E. W. Dijkstra

                          E 1 Reply Last reply
                          0
                          • Pl45m4P Pl45m4

                            @ekato993

                            That should work now. Does it?

                            E Offline
                            E Offline
                            ekato993
                            wrote on last edited by
                            #12

                            @Pl45m4 It does work, i set it as solved. Thanks for your help.

                            1 Reply Last reply
                            1

                            • Login

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