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
Forum Updated to NodeBB v4.3 + New Features

The program has unexpectedly finished

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 465 Views 1 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.
  • T Offline
    T Offline
    thomasGl
    wrote on last edited by Chris Kawa
    #1
    #include "hauptfenster.h"
    
    hauptfenster::hauptfenster( Button *b)
    {
        b->a=new QPushButton(this);
        QLabel *a=new QLabel(this);       //when i delete this it works why?
    }
    
    
    hauptfenster::~hauptfenster()
    {
    
    }
    

    Hey guys. I want a label and a pushbutton in my mainwindow constructor. How can i fix that? Why does it not work??

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi, welcome to the forum.

      You have to give us more detail and information on what is b->a is, what's the relation of b to this, who's parenting it etc.
      Obviously creating a label won't crash your app, so run your app in Debug mode, with debugger attached, and look at the call stack what exactly crashes and at which point.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thomasGl
        wrote on last edited by
        #3

        11.PNG 22.PNG 33.PNG 444.PNG
        555.PNG

        So i am trying to put the Button and the label inside the constructor of my MainWindow. How can i do that? If i delete one of them in the constructor it works, but i need both of them. With the b->a i am getting QPushbutton *a from my Button class and with c->n i am getting the label from the label class. I dont understand why it is not working.

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Please stop posting images but source code!
          And provide a minimal compilable example, not a lot of unrelated stuff. Your example can be stripped down to 20 lines of code in one source file I would guess, so please do so!

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • T thomasGl

            11.PNG 22.PNG 33.PNG 444.PNG
            555.PNG

            So i am trying to put the Button and the label inside the constructor of my MainWindow. How can i do that? If i delete one of them in the constructor it works, but i need both of them. With the b->a i am getting QPushbutton *a from my Button class and with c->n i am getting the label from the label class. I dont understand why it is not working.

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

            @thomasGl

            Button *b;
            Label *c;
            
            c->n = ...;
            b->a = ...;
            

            As @Christian-Ehrlicher has said, if you posted code and not screenshots we would not have to manually type in stuff to bring to your attention.

            Please review C++ basics. I have no understanding of what you are trying to achieve or how you think your code approaches it. But your code shows two uninitialized variables, which are pointers, and you trying to dereference them. This can only lead to undefined behaviour or crashes. Never mind "deleting one of these lines", they are both wrong.

            I doubt whether your Label class should have a QLabel *n member or your Button class should have a QPushButton *a member, but who knows.

            I want a label and a pushbutton in my mainwindow constructor.

            Then perhaps just have a QLabel * and a QPushButton * as member variables of mainwindow/hauptfenster and assign them to newed objects in the constructor. It is not clear why you would want either of your Label or Button classes here.

            1 Reply Last reply
            2
            • T Offline
              T Offline
              thomasGl
              wrote on last edited by
              #6

              @JonB said in The program has unexpectedly finished:

              But your code shows two uninitialized variables,

              Ok. sorry for that.

              JonBJ 1 Reply Last reply
              0
              • T thomasGl

                @JonB said in The program has unexpectedly finished:

                But your code shows two uninitialized variables,

                Ok. sorry for that.

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

                @thomasGl
                There is no need to be "sorry" :) But you should understand why those variables are uninitialized and why that would cause crashes/undefined behaviour.

                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