Copy std :: vector to qvector

I tried to copy the contents of one vector into QVector using the following

 std::copy(source.begin(),source.end(),dest.begin()); 

However, the QVector destination is still empty.

Any suggestions?

+6
source share
3 answers

Take a look:

 std::vector<T> QVector::toStdVector () const QVector<T> QVector::fromStdVector ( const std::vector<T> & vector ) [static] 

From the documents

+12
source

If you are creating a new QVector with the contents of std::vector , you can use the following code as an example:

  std::vector<T> stdVec; QVector<T> qVec = QVector<T>::fromStdVector(stdVec); 
+3
source

As mentioned in other answers, you should use the following method to convert QVector to std::vector :

 std::vector<T> QVector::toStdVector() const 

And the following static method to convert std::vector to 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; vector.push_back(1.2); vector.push_back(0.5); vector.push_back(3.14); QVector<double> vector = QVector<double>::fromStdVector(stdvector); 

Remember to specify the type after the second QVector (this should be QVector<double>::fromStdVector(stdvector) , not QVector::fromStdVector(stdvector) ). This will not result in an annoying compiler error.

0
source

Source: https://habr.com/ru/post/951333/


All Articles