Problems with Open-Source Downloads read and

Math with Qt

  • Gents;

    I have been working on adding two numbers using 2 Qlinedits and displaying the result on a Qlabel using a Qpushbutton. Once launched the .exe file crashes and shows no result: please find below my code for review, your advices are welcome.


    my .h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QSpinBox>
    #include <QLabel>
    #include <QLineEdit>
    namespace Ui {
    class MainWindow;
    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = 0);
        void valueChanged (QString resultat);
    private slots:
        void on_pushButton_clicked();
        Ui::MainWindow *ui;
    QLineEdit *Input1;
    QLineEdit *Input2;
    QLabel *resultat;
    #endif // MAINWINDOW_H

    my .cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        delete ui;
    void MainWindow::valueChanged(QString)
        ui->resultat->setText(QString::number(Input1->text().toInt() + Input2->text().toInt()));
    void MainWindow::on_pushButton_clicked()

  • Hi! Input1 and Input2 point to nothing.

  • @Abderaouf

    Added code tags to your post.
    Wieland was faster to answer

  • Moderators

    the variables Input1 and Input2 are never initialized, but you are accessing them.
    I guess you want the input fields from your ui file no?
    You should access them with ui->Input1

  • Thank you guys and lady. That was reallly helpful.

Log in to reply