Search code examples
qtqt4qlistqvector

Populating QVector from QList


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


Solution

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