gmanzoli
gmanzoli

Reputation: 77

QLineEdit destroyed before his parent

i'm having some trouble executing this code:

void WidgetAggiungi::btnAggiungi_click(){
QDate data=editData->date();

if (radioTelefonata->isChecked()){
    QString numTemp=txtNumero->text();
    ...
}

When i try to access txtNumero i got a segmentation fault. txtNumero is a QLineEdit decleared as a private member of WidgetAggiungi. What bothers me is that the QDateEdit editData works fine even if it's decleared and created in the same way of txtNumero.

All the code of WidgetAggiungi is on github: https://github.com/GiacomoManzoli/qdb/blob/master/widgetaggiungi.h https://github.com/GiacomoManzoli/qdb/blob/master/widgetaggiungi.cpp

Upvotes: 1

Views: 90

Answers (3)

Evan Teran
Evan Teran

Reputation: 90432

You have two problems one of which is serious, the other is a minor memory leak:

Firstly, you created the QLineEdit like this:

    QLineEdit* txtNumero= new QLineEdit();

that is creating a local variable named txtNumero and not assigning it to the class member. You should write this instead:

txtNumero= new QLineEdit();

It appears that you have made this mistake in a few places. You should apply a similar fix to all of them.

Also, you should pass the parent to the constructors so they will automatically get cleaned up. Like this:

txtNumero= new QLineEdit(this);

Hope this helps!

Upvotes: 0

user3301159
user3301159

Reputation:

In your constructor:

QLineEdit* txtNumero = new QLineEdit();

You are declaring a local variable, not instanciating your class member. Just remove the type at the beginning:

txtNumero = new QLineEdit();

Upvotes: 0

andrea.marangoni
andrea.marangoni

Reputation: 1499

you declared txtNumero on the header file. but then in the constructor you did : QLineEdit* txtNumero = new ... so you are not initializing the txtNumero on the header. that's why it gave you segfault..

Upvotes: 2

Related Questions