PDA

View Full Version : dynamic allocation of QlineEdit causing application crash, how to solve it?



CyrilQt
9th June 2012, 21:38
Good evening everyone,

I try to create a form with a number of fields that are set by depending on a parameter.
So I wanted to do dynamic allocation of QLineEdit. I must say I am quite happy at first because the window displays properly with the correct number of fields.
However when I close the application, it does not close properly.
it says that the application encountered a problem, and i should send an error report.
Here's a snippet. H and. Cpp I have abbreviated for you.
Thank you for your valuable advice.




#ifndef CHAMPSVARIABLES_H
#define CHAMPSVARIABLES_H

#include<QtGui>

class ChampsVariables : public QWidget
{
Q_OBJECT

public:
ChampsVariables();
QLineEdit *LabelGenerique;
QVBoxLayout *Layout;
private:

private slots:

};
#endif // CHAMPSVARIABLES_H


.cpp


#include"ChampsVariables.h"

ChampsVariables::ChampsVariables() : QWidget()
{
LabelGenerique = new QLineEdit[5];
Layout = new QVBoxLayout;

for(int i=0;i<5;i++)
{
LabelGenerique[i].setText("");
Layout->addWidget(&LabelGenerique[i]);
}

setLayout(Layout);

QLayoutItem * item;

}


a simple main:


#include"ChampsVariables.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

ChampsVariables fenetre;
fenetre.show();


return app.exec();
}

Zlatomir
9th June 2012, 23:08
Use an array that will hold pointers to QLineEdit


//declare
QLineEdit **LabelGenerique;
//define
ChampsVariables::ChampsVariables(QWidget* parent) : QWidget(parent) //don't forget to pass the parent pointer to QWidget's constructor
{
LabelGenerique = new QLineEdit*[5];
Layout = new QVBoxLayout;

for(int i=0;i<5;i++)
{
LabelGenerique[i] = new QLineEdit();
LabelGenerique[i]->setText("");
Layout->addWidget( LabelGenerique[i] );
}

setLayout(Layout);
//don't forget to delete the array, else you have a small memory leak.

or a much better approach is to use a QList<QLineEdit*> (again pointers to widgets go into the container)

LE: added the parent comment and removed some redundant operator :o

CyrilQt
10th June 2012, 16:04
Thank you very much it works perfectly.
However, I meet new challenges with the rest of my code. I can no longer extract the contents of QLineEdit as before.
Following that worked here before:


QString data_to_insert = "";
for (int i=1;i<5;i++) /
{
data_to_insert +="'" + this->LineEditlGenerique[i].text().toLower() + "'" ;
}

I do not control completely the pointers, I tried several combinations like ** & LineEditGenerique or even &&. but it was not very conclusive.
I am very grateful for all the coming advice.

Zlatomir
10th June 2012, 17:25
You have the example code: LabelGenerique[ i ]->setText(""); so that code must use the "arrow" operator ( -> ): LineEditlGenerique[ i ]->text()//...;
//dereference is not enough you need dereference and then access the member

CyrilQt
10th June 2012, 18:17
thank you a thousand times, it works great :)

oups, just one point ? where should i delete it? when should i delete this array?
because i still have a crash due to this line


data_to_insert +="'" + LineEditlGenerique[i]->text().toLower() + "'" ;

without setting
delete LineEditlGenerique; at the proper place

Zlatomir
10th June 2012, 19:19
As i said in the first post you can save yourself of a lot of trouble if you use QList<QLineEdit*> and not an c-style array.

Anyway that shouldn't cause a crash, just a memory leak, so make sure you don't have other mistakes... i see you have done some renaming there

And second issue that delete instruction should be a delete[] LineEditlGenerique; call (notice []), because you delete an array and you should call that in your class destructor.

CyrilQt
10th June 2012, 19:41
Thanks for your time,
I didn't use QList<QLineEdit*> because your first advice and solution was working good enough for me.
I really appreciate your patience, and will set the delete [] lineEditlGenerique in my destructor.

Best regards.

CyrilQt
11th June 2012, 11:50
it works, thanks for everything!