Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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



  • 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);
                    });


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



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

    connect(button, ???, this,[=] {
         MainWindow::rightClick(i, j);
    });
    


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



  • @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)



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



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



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



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



  • 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);
                    });


  • @ekato993

    That should work now. Does it?



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


Log in to reply