Convert 2D vector to 2D QVector C++

1.4k views Asked by At

I'm currently working on a project in C++ and I'm using Visual Studio 2010 Express along with Qt Creator 5.3.2. for the GUI of the application.

My problem is that I want to convert a 2D vector to a 2D QVector and I don't really know how to do this. I have used, for example, the fromStdVector() for the conversion of a 1D vector to a 1D QVector, but I cannot successfully transfer the data from a 2D vector to a 2D QVector (with this function). If anyone could help, I would much appreciate it.

Source Code:

QVector< QVector<double> > test2D; // 2D QVector
vector < vector<double> >  std_test2D; // 2D vector

test2D.resize(20); // dimensions of the 2D QVector
for(int i=0; i<20; ++i)
{
  test2D[i].resize(44);
}

std_test2D.resize(20); // dimensions of the 2D vector
for(int i=0; i<20; ++i)
{
  std_test2D[i].resize(44);
}

for(int i=0; i<20; i++)  // convert vector to QVector???
{
    test2D[i].fromStdVector(std_test2D[i]);
}
2

There are 2 answers

0
Bowdzone On BEST ANSWER

You're not assigning the value. I think this should work using the example code from the documentation:

 test2D[i] = QVector<double>::fromStdVector(std_test2D[i]);
0
vahancho On

You might try the following:

for(int i = 0; i < 20; i++)
{
    QVector v = QVector<double>::fromStdVector(std_test2D[i]);
    test2D[i].push_back(v);
}