I suggest you use deleteLater() and forget about the problem
According to QtAssistence--> Defination of deleteLater()
"Schedules this object for deletion.
The object will be deleted when control returns to the event loop."
Is it possible to return the control to the event loop immediately after the object is deleted. Or in other words how do I return the control such that my object gets deleted immediately.
Also, since children() gets updated immediately after any object is deleted, I think pointer to an object should also get deleted. I have written a sample program where the deletion happens without any problem.
class myObject;
public:
~mainObject(){}
void deleteTheObject();
};
class myObject : public mainObject{
public:
myObject
( QObject * parent
= 0 ) : mainObject
( parent
){} ~myObject(){}
void deleteSelected(){
delete this;
}
private:
};
void mainObject::deleteTheObject(){
Q_FOREACH( QObject* obj, children
() ){ qDebug() << children().count();
if( obj->objectName() == "c" ){
((myObject*)obj)->deleteSelected();
}
}
}
class myObject;
class mainObject : public QObject{
public:
mainObject( QObject * parent = 0 ) : QObject( parent ){}
~mainObject(){}
void deleteTheObject();
};
class myObject : public mainObject{
public:
myObject( QObject * parent = 0 ) : mainObject( parent ){}
~myObject(){}
void deleteSelected(){
delete this;
}
private:
};
void mainObject::deleteTheObject(){
Q_FOREACH( QObject* obj, children() ){
qDebug() << children().count();
if( obj->objectName() == "c" ){
((myObject*)obj)->deleteSelected();
}
}
}
To copy to clipboard, switch view to plain text mode
Uses
int main(int argc, char *argv[]){
mainObject* ob = new mainObject;
myObject* a = new myObject(ob);
a->setObjectName( "a" );
myObject* b = new myObject(ob);
b->setObjectName( "b" );
myObject* c = new myObject(ob);
c->setObjectName( "c" );
myObject* d = new myObject(ob);
d->setObjectName( "d" );
myObject* e = new myObject(ob);
e->setObjectName( "e" );
ob->deleteTheObject();
return 0;
}
int main(int argc, char *argv[]){
mainObject* ob = new mainObject;
myObject* a = new myObject(ob);
a->setObjectName( "a" );
myObject* b = new myObject(ob);
b->setObjectName( "b" );
myObject* c = new myObject(ob);
c->setObjectName( "c" );
myObject* d = new myObject(ob);
d->setObjectName( "d" );
myObject* e = new myObject(ob);
e->setObjectName( "e" );
ob->deleteTheObject();
return 0;
}
To copy to clipboard, switch view to plain text mode
In the above program, after the object is deleted, children() gets updated, the count is decreased.
I tried modifying the above program with below given statement, But was unsuccessful.
If you delete an object pointed by a pointer, you don't delete the pointer itself, therefore it is still a valid "object" and occupies space in the list.
How do i delete the pointer to an object in QList<QObject*>????
Wysota, can you please clear my funda.
One more small doubt.
In qt3 children() is a typedef for QList<QObject> and in Qt4 it is QList<QObject*>. Can you please clear me about this. what happens when object is deleted in both cases. I read Assistance, but there is still confusion.
Thanks
Bookmarks