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. Program closes too soon
Qt 6.11 is out! See what's new in the release blog

Program closes too soon

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 5 Posters 9.2k Views 4 Watching
  • 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.
  • sneubertS Offline
    sneubertS Offline
    sneubert
    wrote on last edited by
    #4

    at least in the code you posted answer is not initalized and where do you get the pointer to answerbox? shouldn´t this be something like ui->answerbox?

    D 1 Reply Last reply
    1
    • D DocDJ

      Thanks for the superfast reply. Here's the whole program (stripped down to simplest form that fails).

      #include <QtGui>
      #include "djdialog1.h" // note: no "ui_" prefix here
      #include <sstream>
      #include <string>	
      
      int int1, int2, answer;
      djdialog1::djdialog1(QWidget *parent)
          : QDialog(parent)
      	{
      	    setupUi(this); // initialize my form
      	    connect(int1box, SIGNAL(textChanged(const QString &)),
      	        this, SLOT(on_int1box_textChanged()));
      	    connect(int2box, SIGNAL(textChanged(const QString &)),
      	        this, SLOT(on_int1box_textChanged()));
      	  
      	    connect(GoButton, SIGNAL(clicked()), this, SLOT(accept()));
      	}
      void djdialog1::on_GoButton_clicked()
      	{ 
      	    QString qreply;
      	    qreply=QString::number(answer);
      	    answerbox->append(qreply);
      	}
      void djdialog1::on_int1box_textChanged(const QString &arg)
      	{bool ok;
      	int1 = arg.toInt(&ok,10);
      	}
      void djdialog1::on_int2box_textChanged(const QString &arg)
      	{bool ok;
      	int2 = arg.toInt(&ok,10);}
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #5

      @DocDJ To add to @sneubert : did you try to debug? This is the easiest way to find out where the app is crashing.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • sneubertS sneubert

        at least in the code you posted answer is not initalized and where do you get the pointer to answerbox? shouldn´t this be something like ui->answerbox?

        D Offline
        D Offline
        DocDJ
        wrote on last edited by
        #6

        @sneubert Since it compiled without errors or warnings, I wasn't aware it needed a ui->, but I will try it. Thanks for the tip.

        mrjjM 1 Reply Last reply
        0
        • D DocDJ

          @sneubert Since it compiled without errors or warnings, I wasn't aware it needed a ui->, but I will try it. Thanks for the tip.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #7

          @DocDJ

          Hi , you do not need UI but!

          if you just have
          QTextEdit *int1box; in djdialog1.h, you need to allocate a real objects before use. This is just a definition of a pointer.

          // somewhere else
          int1box = new QTextEdit (this);

          You use use Designer then all Widgets u place on a form is available via
          UI->the_name

          Please notice those are only valid after
          setupUi(this);

          D 2 Replies Last reply
          1
          • mrjjM mrjj

            @DocDJ

            Hi , you do not need UI but!

            if you just have
            QTextEdit *int1box; in djdialog1.h, you need to allocate a real objects before use. This is just a definition of a pointer.

            // somewhere else
            int1box = new QTextEdit (this);

            You use use Designer then all Widgets u place on a form is available via
            UI->the_name

            Please notice those are only valid after
            setupUi(this);

            D Offline
            D Offline
            DocDJ
            wrote on last edited by
            #8

            @jsulm said in program closes too soon:

            @DocDJ To add to @sneubert : did you try to debug? This is the easiest way to find out where the app is crashing.

            Not sure how to proceed with debugging this one, as it gives no clue as to a cause. I assumed that creating the int1box, etc in Designer would create the objects in my header file for my c++ code to use.

            jsulmJ 2 Replies Last reply
            0
            • D DocDJ

              @jsulm said in program closes too soon:

              @DocDJ To add to @sneubert : did you try to debug? This is the easiest way to find out where the app is crashing.

              Not sure how to proceed with debugging this one, as it gives no clue as to a cause. I assumed that creating the int1box, etc in Designer would create the objects in my header file for my c++ code to use.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #9

              @DocDJ You said it crashes when your "go" method is called, then put a break point there and debug...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • D DocDJ

                @jsulm said in program closes too soon:

                @DocDJ To add to @sneubert : did you try to debug? This is the easiest way to find out where the app is crashing.

                Not sure how to proceed with debugging this one, as it gives no clue as to a cause. I assumed that creating the int1box, etc in Designer would create the objects in my header file for my c++ code to use.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #10

                @DocDJ If you used designer then you should access your UI objects via ui-> , but you don't.
                If you use designer then you do not add any object pointers to your classes by yourself - this is done for you.
                Instances are created when you call setupUi() and then you can access them via ui->objName.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • mrjjM mrjj

                  @DocDJ

                  Hi , you do not need UI but!

                  if you just have
                  QTextEdit *int1box; in djdialog1.h, you need to allocate a real objects before use. This is just a definition of a pointer.

                  // somewhere else
                  int1box = new QTextEdit (this);

                  You use use Designer then all Widgets u place on a form is available via
                  UI->the_name

                  Please notice those are only valid after
                  setupUi(this);

                  D Offline
                  D Offline
                  DocDJ
                  wrote on last edited by
                  #11

                  @mrjj said in program closes too soon:

                  @DocDJ

                  Hi , you do not need UI but!

                  if you just have
                  QTextEdit *int1box; in djdialog1.h, you need to allocate a real objects before use. This is just a definition of a pointer.

                  // somewhere else
                  int1box = new QTextEdit (this);

                  You use use Designer then all Widgets u place on a form is available via
                  UI->the_name

                  Please notice those are only valid after
                  setupUi(this);

                  In "setupui, this line: answerbox = new QTextEdit(djdialog1); creates the object. Adding Ui-> in front of answerbox causes an error.

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DocDJ
                    wrote on last edited by kshegunov
                    #12

                    It would appear that setupUi is not being called. Here is my main.cpp:

                    #include <QApplication>
                    // following 2 lines are initially here to build the "project"
                    //#include <QDialog>
                    //#include "ui_djdialog1.h"
                    // following line replaces the above 2 lines
                    #include "djdialog1.h"
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication app(argc, argv);
                        /** original code before 1st qmake-qt4 my.pro
                        Ui::djdialog1 ui;  // define an object named "ui" as being a djdialog1 
                        QDialog *mydialog = new QDialog;
                        ui.setupUi(mydialog);
                        */
                        // following line is new (after initial make)
                        djdialog1 *mydialog = new djdialog1;
                    	mydialog->show();
                        return app.exec();
                    }
                    

                    I got this from a Qt tutorial.

                    [Added code tags ~kshegunov]

                    kshegunovK 1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      Hi
                      It seems you did already call it the normal place ?
                      djdialog1::djdialog1(QWidget *parent)
                      : QDialog(parent)
                      {
                      setupUi(this); // initialize my form <<<<<<<<<<<<<<<<<<<

                      In the ctor.
                      Outside and in main is very unusual :)

                      D 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        Hi
                        It seems you did already call it the normal place ?
                        djdialog1::djdialog1(QWidget *parent)
                        : QDialog(parent)
                        {
                        setupUi(this); // initialize my form <<<<<<<<<<<<<<<<<<<

                        In the ctor.
                        Outside and in main is very unusual :)

                        D Offline
                        D Offline
                        DocDJ
                        wrote on last edited by
                        #14

                        @mrjj The call to setupUi in main was commented-out, so it doesn't happen twice. Maybe I need to do it in main, instead of the dialog. I will have read about how to set a breakpoint in Qt4.

                        mrjjM jsulmJ 2 Replies Last reply
                        0
                        • D DocDJ

                          @mrjj The call to setupUi in main was commented-out, so it doesn't happen twice. Maybe I need to do it in main, instead of the dialog. I will have read about how to set a breakpoint in Qt4.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #15

                          @DocDJ
                          Well you can create the default desktop widget application (File -> New Project )
                          and see if that includes the setupUi in mainwindow constructor.

                          1 Reply Last reply
                          0
                          • D DocDJ

                            @mrjj The call to setupUi in main was commented-out, so it doesn't happen twice. Maybe I need to do it in main, instead of the dialog. I will have read about how to set a breakpoint in Qt4.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            @DocDJ Sorry, but this is just completely wrong:

                            Ui::djdialog1 ui; // define an object named "ui" as being a djdialog1
                            QDialog *mydialog = new QDialog;
                            ui.setupUi(mydialog);
                            

                            You call setupUi() in the window/dialog you create. That means djdialog1 calls it in its constructor (what you're already doing). main is not the correct place to call setupUI. You really should learn Qt basics first, what you are doing now is just guessing and is wrong.
                            You can create a default widgets project edit the main window, add a dialog and check the generated code.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            kshegunovK 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @DocDJ Sorry, but this is just completely wrong:

                              Ui::djdialog1 ui; // define an object named "ui" as being a djdialog1
                              QDialog *mydialog = new QDialog;
                              ui.setupUi(mydialog);
                              

                              You call setupUi() in the window/dialog you create. That means djdialog1 calls it in its constructor (what you're already doing). main is not the correct place to call setupUI. You really should learn Qt basics first, what you are doing now is just guessing and is wrong.
                              You can create a default widgets project edit the main window, add a dialog and check the generated code.

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on last edited by kshegunov
                              #17

                              @jsulm said in program closes too soon:

                              Sorry, but this is just completely wrong:

                              Actually it's correct. I often initialize the widgets through forms without deriving. The most typical example being:

                              QMainWindow window;
                              Ui::MyMainWindowForm ui;
                              ui.setupUi(&window);
                              
                              window.show;
                              

                              Notice that the initialized widget is a generic one (as with his code).

                              PS. And by the way this is commented out.

                              Kind regards.

                              Read and abide by the Qt Code of Conduct

                              jsulmJ 1 Reply Last reply
                              0
                              • D DocDJ

                                It would appear that setupUi is not being called. Here is my main.cpp:

                                #include <QApplication>
                                // following 2 lines are initially here to build the "project"
                                //#include <QDialog>
                                //#include "ui_djdialog1.h"
                                // following line replaces the above 2 lines
                                #include "djdialog1.h"
                                
                                int main(int argc, char *argv[])
                                {
                                    QApplication app(argc, argv);
                                    /** original code before 1st qmake-qt4 my.pro
                                    Ui::djdialog1 ui;  // define an object named "ui" as being a djdialog1 
                                    QDialog *mydialog = new QDialog;
                                    ui.setupUi(mydialog);
                                    */
                                    // following line is new (after initial make)
                                    djdialog1 *mydialog = new djdialog1;
                                	mydialog->show();
                                    return app.exec();
                                }
                                

                                I got this from a Qt tutorial.

                                [Added code tags ~kshegunov]

                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on last edited by
                                #18

                                @DocDJ
                                Please use the triple backtick to get the code formatted, so people don't get confused and you don't lose any symbols due to forum markup.

                                Read and abide by the Qt Code of Conduct

                                1 Reply Last reply
                                0
                                • kshegunovK kshegunov

                                  @jsulm said in program closes too soon:

                                  Sorry, but this is just completely wrong:

                                  Actually it's correct. I often initialize the widgets through forms without deriving. The most typical example being:

                                  QMainWindow window;
                                  Ui::MyMainWindowForm ui;
                                  ui.setupUi(&window);
                                  
                                  window.show;
                                  

                                  Notice that the initialized widget is a generic one (as with his code).

                                  PS. And by the way this is commented out.

                                  Kind regards.

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19

                                  @kshegunov You're right - it is not wrong, but it is not how it is usually done (at least what I saw so far).
                                  Isn't setupUi() called in the constructor already?

                                  djdialog1::djdialog1(QWidget *parent)
                                      : QDialog(parent)
                                  	{
                                  	    setupUi(this); // initialize my form
                                  

                                  @DocDJ "Adding Ui-> in front of answerbox causes an error." - it is ui-> not Ui->

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  kshegunovK 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @kshegunov You're right - it is not wrong, but it is not how it is usually done (at least what I saw so far).
                                    Isn't setupUi() called in the constructor already?

                                    djdialog1::djdialog1(QWidget *parent)
                                        : QDialog(parent)
                                    	{
                                    	    setupUi(this); // initialize my form
                                    

                                    @DocDJ "Adding Ui-> in front of answerbox causes an error." - it is ui-> not Ui->

                                    kshegunovK Offline
                                    kshegunovK Offline
                                    kshegunov
                                    Moderators
                                    wrote on last edited by kshegunov
                                    #20

                                    @jsulm said in program closes too soon:

                                    but it is not how it is usually done

                                    True, I'm only remarking it's a valid approach. I actually don't advise its usage for beginners.

                                    Isn't setupUi() called in the constructor already?

                                    No, because the object in question is of type QDialog. There's duplication of names - the same name is used for the form and the custom dialog, so I think this is where the confusion stems from.

                                    @DocDJ
                                    Please provide the header file for your class as well. I have a strong suspicion you're deriving both from the dialog and the ui form.

                                    Read and abide by the Qt Code of Conduct

                                    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