The program has unexpectedly finished
-
#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??
-
Hi, welcome to the forum.
You have to give us more detail and information on what is
b->ais, what's the relation ofbtothis, 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. -
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.
-
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! -
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.
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
Labelclass should have aQLabel *nmember or yourButtonclass should have aQPushButton *amember, but who knows.I want a label and a pushbutton in my mainwindow constructor.
Then perhaps just have a
QLabel *and aQPushButton *as member variables of mainwindow/hauptfensterand assign them tonewed objects in the constructor. It is not clear why you would want either of yourLabelorButtonclasses here. -
@JonB said in The program has unexpectedly finished:
But your code shows two uninitialized variables,
Ok. sorry for that.