Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The program has unexpectedly finished.

The program has unexpectedly finished.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nanor
    wrote on last edited by nanor
    #1

    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_H
    

    dialog.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":
    1.png
    after clicking on the button "click me":
    2.png
    The arrow appears after debugging:
    3.png

    1 Reply Last reply
    0
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      You're doing variable shadowing, by shadowing member variable in local scopes.

      https://www.learncpp.com/cpp-tutorial/variable-shadowing-name-hiding/


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      5
      • N Offline
        N Offline
        nanor
        wrote on last edited by
        #3

        @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.HilkJ 1 Reply Last reply
        0
        • N nanor

          @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.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @nanor
          from
          QLabel *labela = new QLabel(this);
          ^
          new scoped only inside the function accessible variable

          to
          labela = new QLabel(this);
          ^
          actually in the header defined variable and in all of Dialog.cpp accessible


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          JonBJ 1 Reply Last reply
          2
          • N Offline
            N Offline
            nanor
            wrote on last edited by
            #5

            @J-Hilk Thank you so much. now my code works fine. Really appreciate your help.

            1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @nanor
              from
              QLabel *labela = new QLabel(this);
              ^
              new scoped only inside the function accessible variable

              to
              labela = new QLabel(this);
              ^
              actually in the header defined variable and in all of Dialog.cpp accessible

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @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... :)

              J.HilkJ 1 Reply Last reply
              0
              • JonBJ JonB

                @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... :)

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @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


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                JonBJ 1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  @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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @J-Hilk
                  Ooohhh :) That's interesting, didn't know. I compile with -Wall but 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... :)

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved