tags:

views:

14

answers:

1

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"
+1  A: 

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.

chalup
I seems that there must be some kind of automatic connection of signals? I thought you had to specify the connection in qt designer, once I removed the manually added link in qt designer it behaves like I expected! thanks
Chris Camacho
@Chris: Yes, you get automatic connection of signals to slots if the slot is named "on_<emitting object>_<signal>", and has the correct parameters.
Caleb Huitt - cjhuitt