本文介绍了从QList填充QVector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QList和QVector.我填充Qlist,然后尝试复制到QVector. Ovector具有 fromList()方法.但这不起作用.

I have a QList and QVector. I populate Qlist and then tried to copy to QVector. Ovector has a fromList() method. But it do not work.

我的代码:

QList<int> listA;
QVector<int> vectorA; //I also tried QVector<int>vectorA(100);

for(int i=0; i<100; i++)
{
    listA.append(i);
}

vectorA.fromList(listA);

此代码之后vectorA返回空

After this code vectorA returns empty

我在Linux下使用Qt 4.8.5

I use Qt 4.8.5 under Linux

推荐答案

您应输入:

vectorA = QVector::fromList(listA);

as fromList 是类别QVector.

您编写的代码从listA创建了QVector,但是由于您没有使用赋值运算符将其存储在某个变量中,也没有通过例如函数调用使用它,因此QVector被删除.无论哪种方式,vectorA都为空.

The code you wrote create a QVector from listA but as you do not store it in some variable using the assignment operator or use it through a function call for instance, the QVector is dropped. Either way, vectorA remains empty.

或者,您可以在以下内容中使用 QList::toVector 方式:

Alternatively, you can use QList::toVector the following way:

vectorA = listA.toVector();

这篇关于从QList填充QVector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 21:50