我主要是从Here复制、粘贴代码,然后
在一个小的新程序中实现它们,如下所示
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();
};
并在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()));
onRightClicked
插槽是这样的:
void MainWindow::onRightClicked()
{
qDebug() << "clicked";
}
但是调试出来的只有这个:Emitted
.
我不知道这里哪里错了。那么我该如何解决这个问题呢?
谢谢。