[Qt-interest] QTextEdit not deleted when implemented as list

ross at earz.ca ross at earz.ca
Thu Jun 23 16:48:36 CEST 2011


> Dear All
>
> I have list of QTextEdit, I am creating the edit box and then trying to
> delete but its not deleting. Can u anyone  tell me what I am doing
> wrong.
>
> ////////////////////////////////////////////////////////////////////////
> //////////////////////////////////////
>
> QList<QTextEdit *> list;
>
> int w = 100;
>
> int h= 25;
>
> int incre=0;
>
> for(i=0; i<2; i++)
>
> {
>
>     list.append(new QTextEdit(this));

Once you have created a QWidget on the heap and given it a parent, its
deletion will occur automatically when the parent is deleted.


>
>     list.at(i)->setGeometry(0,0+incre,w,h);
>
>    list.at(i)->show();
>
>    incre = incre+h+10;
>
> }
>
>
>
> int cnt = list.count();
>
> for(int i=0; i<cnt; i++)
>
> delete list.at(i);

Strictly speaking, this is not required, but if you do want to clean it up
now, I suggest that you call deleteLater() on the QTextEdit pointer and
not delete. In using the C++ operator you run the risk of a signal being
sent to a slot on a deleted object which will crash your app at
intermittent times.

> list.clear();
>
> ////////////////////////////////////////////////////////////////////////
> ////////////////////
>
> But even after deleting the edit boxes I can still see them, means
> they're not being deleted. Whats wrong , any help would be appreciated.

I would suggest that you call hide() on the text edits and then call
deleteLater() if you want to clean up the memory early.  Perhaps your loop
should look more like this:


for(int i=0; i<cnt; i++)
{
    QTextEdit p = list.at(i);
    p->hide();
    p->deleteLater();
}


On an aside note, if you are transversing a QList, you will probably have
more efficiency using iterators and not an index.

R.





More information about the Qt-interest-old mailing list