将清单转换为QVariant或QVariant清单 [英] Casting a list as QVariant or QVariant List

查看:148
本文介绍了将清单转换为QVariant或QVariant清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是这个.我有不同数值类型的列表,例如:

My problem is this. I have lists of different numeric types, for example:

QList<qreal> mylist;

现在,在我的代码中,我有一个函数,它期望一个QVariant参数为mylist.我发现做到这一点的唯一方法是使用for cyle并简单地将mylist中的所有数据添加到第二个列表中

Now, in my code I have a function that that expects a QVariant argument which is mylist. The only way I've found to do this is by using a for cyle and simply adding all of the data in mylist to second list

QList<QVariant> temp, 

例如,并将temp作为参数传递.

for example, and passing temp as a paramter.

我想知道是否还有其他方法可以做到这一点.

I was wondering if there was any other way to do this.

非常感谢您.

推荐答案

我不得不做几次,据我所知,唯一的方法是创建一个新列表.

I've had to do this several times, and to my knowledge the only way is to create a new list.

如果这是您必须经常处理的各种类型的列表,则可以做一些更奇特的操作:

If this is something you have to do frequently with varying types of lists you could do something a little fancier:

template <typename T>
QVariantList toVariantList( const QList<T> &list )
{
    QVariantList newList;
    foreach( const T &item, list )
        newList << item;

    return newList;
}

这样,当您调用包含在QVariant列表中的函数时,您只需调用

so that when you call your function that takes in the QVariant list, you can just call

myFunction( toVariantList(myList) );

请注意,给定函数仅适用于可以隐式转换为QVariants的类型.

Note that the given function will only work for types that can be implicitly converted to QVariants.

这篇关于将清单转换为QVariant或QVariant清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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