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 QPushButtonconnect(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); }); -
Qt new signal and slot method: link
I am using the following connection method. Button is a
QPushButton *button = new QPushButtonconnect(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.
-
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. -
Qt new signal and slot method: link
I am using the following connection method. Button is a
QPushButton *button = new QPushButtonconnect(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.
There was a similar issue / question before.
Go through this topic.
https://forum.qt.io/topic/118098/pushbutton-crashedI 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
MyQPushButtonclassMyQPushButton.hfile#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.cppfile#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.cppfileMyQPushButton *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
rightClickCellandleftClickCellfunction. The left clicking is an option for me, but I do not get any outputs, right click is disabled for me. -
I have created a
MyQPushButtonclassMyQPushButton.hfile#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.cppfile#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.cppfileMyQPushButton *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
rightClickCellandleftClickCellfunction. The left clicking is an option for me, but I do not get any outputs, right click is disabled for me. -
I have maded the MyQPushButton class.
mainwindow.himportant part:public: void createGrid(); public slots: void myButtonClicked(QMouseEvent *e, int row, int col);mainwindow.cppimportant 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 = nullptrcause me crash. I do not know how to useQMouseEventto connection. -
I have maded the MyQPushButton class.
mainwindow.himportant part:public: void createGrid(); public slots: void myButtonClicked(QMouseEvent *e, int row, int col);mainwindow.cppimportant 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 = nullptrcause me crash. I do not know how to useQMouseEventto 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 themousePressEventfrom youQPushButtonclass 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); }); -
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); });