PDA

View Full Version : QVariant with QString / who free's its data?



elias.bachaalany
26th October 2010, 10:21
Hello,

Suppose I have the following:

QAction *a = ....

a->setData(QString("Hello world"))
QObject::connect(a, ....)

Later in my slot:

QAction *a = QObject::sender();

QString s = a->data()->toString()

This works fine. But I wonder who takes care of disposing the associated action data (in this case the QString)

Lykurg
26th October 2010, 10:42
Qt uses implicit sharing in some classes. So it does for QString:

Foo::Foo()
{
QAction *a = QAction(this);
a->setData(QString("Hello world"));
}

Foo::~Foo()
{
// a gets destroyed and so also the string since a takes care of that. (And it the reference count is equal 0.
}

Foo::mySlot()
{
QString s = a->data()->toString(); // s is not a real copy (as long as you don't change it
} // s gets deleted (or the reference count gets decreased.