han*_*dle 0 c++ qt casting qvariant qlist
我正在使用QObject的动态属性来存储要在可以访问所述属性的Slot中使用的信息.发件人是一个QState:myQState->setProperty("key", QList<int>(0, 1, 2));
我想将存储的QVariant转换回QList,以便可以迭代.以下代码不起作用(错误C2440:QVariant无法使用{[T = int])转换为QList:
QVariant vprop = obj->property("key");
QList<int> list = vprop; // < - - - - - - - - ERROR
foreach (int e, list )
{
qDebug() << __FUNCTION__ << "" << e;
}
Run Code Online (Sandbox Code Playgroud)
这段代码有效.要设置为属性的对象:
QVariantList list;
list.append(0);
list.append(1);
list.append(2);
Run Code Online (Sandbox Code Playgroud)
并在插槽中
QObject *obj = this->sender();
foreach( QByteArray dpn, obj->dynamicPropertyNames() )
{
qDebug() << __FUNCTION__ << "" << dpn;
}
QVariant vprop = obj->property("key");
qDebug() << __FUNCTION__ << "" << vprop;
QVariantList list = vprop.toList();
foreach(QVariant e, list )
{
qDebug() << __FUNCTION__ << "" << e.toInt();
}
Run Code Online (Sandbox Code Playgroud)
或者使用 QList<QVariant> QVariant::toList () const
QList<QVariant> list = vprop.toList();
Run Code Online (Sandbox Code Playgroud)
然后迭代项目并在需要时将每个项目转换为整数.
| 归档时间: |
|
| 查看次数: |
3186 次 |
| 最近记录: |