Populating QVector from QList

六眼飞鱼酱① 提交于 2020-01-04 08:16:28

问题


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.

My code :

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

After this code vectorA returns empty

I use Qt 4.8.5 under Linux


回答1:


You should write:

vectorA = QVector::fromList(listA);

as fromList is a static method of the class QVector.

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.

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

vectorA = listA.toVector();


来源:https://stackoverflow.com/questions/19569873/populating-qvector-from-qlist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!