I mostly copied, pasted the code from Here, and implemented them in a small new program like this:
In mybutton.h
:
class MyButton : public QPushButton
{
Q_OBJECT
public:
MyButton(QWidget *parent = Q_NULLPTR);
QVector<MyButton*> buttons;
private slots:
void mousePressEvent(QMouseEvent *e) {
if(e->button()==Qt::RightButton) {
emit btnRightClicked();
qDebug() << "Emitted";
}
}
signals:
void btnRightClicked();
};
And in mainwindow.cpp
:
MyButton mButtons;
QWidget *mWidget = new QWidget(this);
QGridLayout *gLayout = new QGridLayout(mWidget);
mButtons.buttons.resize(5);
for(int i = 0; i < 5; i++) {
mButtons.buttons[i] = new MyButton(mWidget);
gLayout->addWidget(mButtons.buttons[i], 0, i);
}
mWidget->setLayout(gLayout);
setCentralWidget(mWidget);
connect(&mButtons, SIGNAL(btnRightClicked()), this, SLOT(onRightClicked()));
And the onRightClicked
slot is like this:
void MainWindow::onRightClicked()
{
qDebug() << "clicked";
}
But the debug out come only has this: Emitted
.
I do not know where is wrong here. So how can I solve the problem?
Thanks.