Store QPointer into a QVariant

640 Views Asked by At

Can I store a QPointer, for example a QPointer<QTcpSocket> inside a QVariant and later extract it from it?

I tried with:

    QObject *ob = new QObject();
    QPointer<QObject> qp(ob);
    QVariant qv(qp);

But I got an error - QVariant::QVariant(void*)' is private.

1

There are 1 best solutions below

0
On

After some more research, it is possible by using QVariant::fromValue() and QVariant::value().

Example code:

    QTcpSocket *ob = new QTcpSocket();
    qDebug("%p", ob);
    QPointer<QTcpSocket> qp(ob);
    QVariant qv = QVariant::fromValue(qp);
    qp = qv.value<QPointer<QTcpSocket> >();
    qDebug("%p", qp.data());
    delete ob;
    qDebug("%p", qp.data());

This gives:

0x137c070
0x137c070
0x0