使用 QPushButton 的自定义插槽时,为什么会收到两次单击或释放信号?

发布于 2024-08-25 12:50:49 字数 1291 浏览 5 评论 0 原文

这是主要代码,起初我认为是消息框,但设置标签具有相同的效果。

#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"

here's the main code at first I thought is was the message box but setting a label instead has the same effect.

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-09-01 12:50:49

您描述的行为通常意味着同一信号和插槽之间有两个连接。确保“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文