qtcontainersqt5qobjectself-destruction

QObject selfdestruction aware container


Is there a Qt container, that is aware of the destroyed signal of QObject and removes the element if an element was destroyed?

I.e. like this:

QObject *obj1 = new MyObject();
QObject *obj2 = new MyObject();
QObjectContainer c;
c.add(obj1);
c.add(obj2);
qDebug() << c.size(); // Prints 2
delete obj1;
qDebug() << c.size(); // Prints 1

Solution

  • You can do it yourself by catching signal destroyed() inside container-derived class.

    P.S. No Qt class does this, AFAIK.