在作为QList的QVariant上迭代< int> [英] Iterating over a QVariant that is a QList<int>?

查看:1917
本文介绍了在作为QList的QVariant上迭代< int>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用QObject的动态属性来存储要在可以访问所述属性的插槽中使用的信息。发送者是一个QState,具有: myQState-> setProperty(key,QList< int>(0,1,2));

I'm using QObject's dynamic property to store information to be used in a Slot that can access said property. The sender is a QState with: myQState->setProperty("key", QList<int>(0, 1, 2));

我要转换存储的 QVariant 返回一个QList,所以它可以迭代。以下代码不工作(错误C2440:QVariant无法转换为与{[T = int])的QList:

I would like to convert the stored QVariant back to a QList so it can be iterated. The following code does not work (error C2440: QVariant can't be converted to QList with {[T=int]):

QVariant vprop = obj->property("key");
QList<int> list = vprop; //   < - - - - - - - - ERROR
foreach (int e, list )
{
    qDebug() << __FUNCTION__ << "" << e;
}






设置为属性的对象:


This code works. The object to set as property:

QVariantList list;
list.append(0);
list.append(1);
list.append(2);

并在插槽

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();
}


推荐答案

c> QList< QVariant> QVariant :: toList()const

Or use QList<QVariant> QVariant::toList () const

QList<QVariant> list = vprop.toList();

而不是遍历这些项目,并根据需要将其转换为整数。

And than iterate over the items and convert each to integer if needed.

这篇关于在作为QList的QVariant上迭代&lt; int&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆