QPushButtonのカスタムスロットを使用するときに、なぜ私は2つのクリックまたは解放信号を得るのですか?

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"
役に立ちましたか?

解決

は、通常、記載された動作は同じ信号とスロットの間に2つのコネクトが存在することを意味します。そのことを確認してください "QMetaObject :: connectSlotsByName(メインウィンドウ);" setupUi(で生成)のみリリース()信号と、カスタムスロットの間に接続されます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top