Solved 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.
Cheers.
my .h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSpinBox> #include <QLabel> #include <QLineEdit> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void valueChanged (QString resultat); private slots: void on_pushButton_clicked(); private: 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) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::valueChanged(QString) { ui->resultat->setText(QString::number(Input1->text().toInt() + Input2->text().toInt())); } void MainWindow::on_pushButton_clicked() { valueChanged("resultat"); }
-
Hi! Input1 and Input2 point to nothing.
-
Added code tags to your post.
Wieland was faster to answer -
@Abderaouf
the variablesInput1
andInput2
are never initialized, but you are accessing them.
I guess you want the input fields from your ui file no?
You should access them withui->Input1
-
Thank you guys and lady. That was reallly helpful.