使用 QPushButton 的自定义插槽时,为什么会收到两次单击或释放信号?
这是主要代码,起初我认为是消息框,但设置标签具有相同的效果。
#include <time.h>
#include "ui_mainwindow.h"
#include <QMessageBox>
class MainWindow : public QWidget, private Ui::MainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
void makeSum(void);
private:
int r1;
int r2;
private slots:
void on_pushButton_released(void);
};
MainWindow::MainWindow(QWidget *parent) : QWidget(parent) {
setupUi(this);
}
void MainWindow::on_pushButton_released(void) {
bool ok;
int a = lineEdit->text().toInt(&ok, 10);
if (ok) {
if (r1+r2==a) {
QMessageBox::information( this, "Sums","Correct!" );
} else {
QMessageBox::information( this, "Sums","Wrong!" );
}
} else {
QMessageBox::information( this, "Sums","You need to enter a number" );
}
makeSum();
}
void MainWindow::makeSum(void) {
r1 = rand() % 10 + 1;
r2 = rand() % 10 + 1;
label->setText(QString::number(r1));
label_3->setText(QString::number(r2));
}
int main(int argc, char *argv[]) {
srand ( time(NULL) );
QApplication app(argc, argv);
MainWindow mw;
mw.makeSum();
mw.show();
return app.exec();
}
#include "main.moc"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您描述的行为通常意味着同一信号和插槽之间有两个连接。确保“QMetaObject::connectSlotsByName(MainWindow);” setupUi() 中生成的信号是released() 信号和自定义插槽之间的唯一连接。
Behavior you described usually means that there are two connects between the same signal and slot. Make sure that "QMetaObject::connectSlotsByName(MainWindow);" generated in setupUi() is the only connect between released() signal and your custom slot.