The program has unexpectedly finished.
-
I am supposed to have a simple program, where after clicking on a button, the multiplication of two numbers typed in lineEdits happens. In the following code, I have created the "clickmebutton" in the constructor, that when the user clicks on it, some labels and lineEdits and the "calculate button" appear. After clicking on the "calculate button", the multiplication of two numbers inserted in lineEdits must be shown in the "answer label (the pink label)" . I have written the following code, after clicking on the "calculate button" , nothing happens and I got the error "The program has unexpectedly finished".
I have also run the program in debug mod and after debugging, an arrow appears near the "linea = lineedita->text();" which is declared in the showtheanswer() function.
I would appreciate if someone could help me solve this problem.
Here is the code:dialog.h:
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QLabel> #include <QPushButton> #include <QLineEdit> #include <QString> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public slots: void showme(); void showtheanswer(); public: explicit Dialog(QWidget *parent = 0); ~Dialog(); QPushButton *clickmebutton; QLabel *labela; QLabel *labelb; QLineEdit *lineedita; QLineEdit *lineeditb; QPushButton *calculate; QLabel *answer; QString linea; QString lineb; double lineanum, linebnum, theanswer; private: Ui::Dialog *ui; }; #endif // DIALOG_Hdialog.cpp:
#include "dialog.h" #include "ui_dialog.h" #include <QPushButton> #include <QDebug> #include <QLabel> #include <QLineEdit> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); QPushButton *clickmebutton = new QPushButton(this); clickmebutton->setGeometry(450,50,100,30); clickmebutton->setText("clickme"); connect(clickmebutton, &QPushButton::clicked,this,Dialog::showme); } Dialog::~Dialog() { delete ui; } void Dialog::showme() { qDebug() << "clicked"; QLabel *labela = new QLabel(this); labela->setGeometry(50,100,30,30); labela->setText("a"); labela->show(); QLabel *labelb = new QLabel(this); labelb->setGeometry(50,200,30,30); labelb->setText("b"); labelb->show(); QLineEdit *lineedita = new QLineEdit(this); lineedita->setGeometry(200,100,100,30); lineedita->show(); QLineEdit *lineeditb = new QLineEdit(this); lineeditb->setGeometry(200,200,100,30); lineeditb->show(); QPushButton *calculate = new QPushButton(this); calculate->setGeometry(50,300,100,30); calculate->setText("calculate"); calculate->show(); QLabel *answer = new QLabel(this); answer->setGeometry(200,300,100,30); answer->show(); answer->setObjectName("text_Name"); answer->setStyleSheet( " QLabel#text_Name {" " background-color: pink;" "}"); connect(calculate,&QPushButton::clicked, this, Dialog::showtheanswer); } void Dialog::showtheanswer() { QString linea; QString lineb; double lineanum, linebnum, theanswer; linea = lineedita->text(); lineb = lineeditb->text(); lineanum = linea.toDouble(); linebnum = lineb.toDouble(); theanswer = lineanum * linebnum; answer->setText(QString::number(theanswer)); }Here is my GUI output:
before clicking the button "click me":

after clicking on the button "click me":

The arrow appears after debugging:

-
You're doing variable shadowing, by shadowing member variable in local scopes.
https://www.learncpp.com/cpp-tutorial/variable-shadowing-name-hiding/
-
@J-Hilk Thank you for replying. I read about the shadowing member variable (I didn't know about it before). But still I have no idea how to fix the problem. Could you please help me on this matther. Should I change the variable names or define some functions inside the showtheanswer() function? Thank you.
-
@J-Hilk Thank you for replying. I read about the shadowing member variable (I didn't know about it before). But still I have no idea how to fix the problem. Could you please help me on this matther. Should I change the variable names or define some functions inside the showtheanswer() function? Thank you.
-
@nanor
from
QLabel *labela = new QLabel(this);
^
new scoped only inside the function accessible variableto
labela = new QLabel(this);
^
actually in the header defined variable and in all of Dialog.cpp accessible -
@J-Hilk
This is why I have said before I wish C++ compilers had an option for warning on declaring a shadow variable, like my beloved C# does, but I have been shouted down here previously for even suggesting this... :)@JonB said in The program has unexpectedly finished.:
This is why I have said before I wish C++ compilers had an option for warning on declaring a shadow variable, like my beloved C# does, but I have been shouted down here previously for even suggesting this... :)
some do, IIRC gcc has
-Wshadow -
@JonB said in The program has unexpectedly finished.:
This is why I have said before I wish C++ compilers had an option for warning on declaring a shadow variable, like my beloved C# does, but I have been shouted down here previously for even suggesting this... :)
some do, IIRC gcc has
-Wshadow@J-Hilk
Ooohhh :) That's interesting, didn't know. I compile with-Wallbut it doesn't seem to get included in that. https://stackoverflow.com/a/57079021/489865 is interesting in explaining various "shadow levels".Of course, as soon as I actually used it I'd probably moan about the perfectly intentional places I find convenient to have a same-named variable and don't want to be warned... :)