copying a std::vector to a qvector
Solution 1
Look at:
std::vector<T> QVector::toStdVector () const
QVector<T> QVector::fromStdVector ( const std::vector<T> & vector ) [static]
Solution 2
If you are creating a new QVector
with the contents of a std::vector
you can use the following code as an example:
std::vector<T> stdVec;
QVector<T> qVec = QVector<T>::fromStdVector(stdVec);
Solution 3
'fromStdVector' has been explicitly marked deprecated recently. Use following code:
std::vector<...> stdVec;
// ...
QVector<...> qVec = QVector<...>(stdVec.begin(), stdVec.end());
Solution 4
As the other answers mentioned, you should use the following method to convert a QVector
to a std::vector
:
std::vector<T> QVector::toStdVector() const
And the following static method to convert a std::vector
to a QVector
:
QVector<T> QVector::fromStdVector(const std::vector<T> & vector)
Here is an example of how to use QVector::fromStdVector
(taken from here):
std::vector<double> stdvector;
stdvector.push_back(1.2);
stdvector.push_back(0.5);
stdvector.push_back(3.14);
QVector<double> vector = QVector<double>::fromStdVector(stdvector);
Don't forget to specify the type after the second QVector
(it should be QVector<double>::fromStdVector(stdvector)
, not QVector::fromStdVector(stdvector)
). Not doing so will give you an annoying compiler error.
Related videos on Youtube

Rajeshwar
Updated on June 12, 2020Comments
-
Rajeshwar over 2 years
I tried copy content of one vector to a
QVector
using the followingstd::copy(source.begin(), source.end(), dest.begin());
However the destination
QVector
still is empty.Any suggestions ?
-
Praetorian over 9 years
-
-
Rajeshwar over 9 yearstried that. as
MyQvector.fromStdVector(MyStedVector);
still the qvector is empty -
user1837009 over 9 years@Rajeshwar: because it static method, try: MyQvector = QVector::fromStdVector( MyStedVector );
-
Kafka about 2 yearsI really don't see why QT team has deprecated this function. it seems quite useful and more easy to write in my mind ...