【问题标题】:Store QPointer into a QVariant将 QPointer 存储到 QVariant 中
【发布时间】:2019-06-26 15:04:22
【问题描述】:
我可以在QVariant 中存储QPointer,例如QPointer<QTcpSocket>,然后再从中提取吗?
我试过了:
QObject *ob = new QObject();
QPointer<QObject> qp(ob);
QVariant qv(qp);
但我遇到了一个错误 - QVariant::QVariant(void*)' is private。
【问题讨论】:
标签:
c++
qt
qvariant
qpointer
【解决方案1】:
经过更多研究,可以使用QVariant::fromValue() 和QVariant::value()。
示例代码:
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());
这给出了:
0x137c070
0x137c070
0x0