问题
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