QPushButton의 사용자 정의 슬롯을 사용할 때 두 번 클릭 또는 릴리스 신호를받는 이유는 무엇입니까?

StackOverflow https://stackoverflow.com/questions/2474561

  •  21-09-2019
  •  | 
  •  

문제

다음은 처음에는 메인 코드가 있습니다. 제가 메시지 상자라고 생각했지만 레이블을 설정하는 대신 동일한 효과가 있습니다.

#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"
도움이 되었습니까?

해결책

당신이 설명한 동작은 일반적으로 동일한 신호와 슬롯 사이에 두 개의 연결이 있음을 의미합니다. "qmetaobject :: connectslotsbyName (MainWindow);" setupui ()에서 생성 된 것은 Releasion () 신호와 사용자 정의 슬롯 사이의 유일한 연결입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top