How to use QVectorQVector double as array?

3

In mainwindow.h I have:

private:
QVector<QVector<double> > numbers; //Variável que será minha matriz
public slot:
void realizar_calcs(QVector<QVector<double> > &numbers);

Na mainwindow.cpp

void MainWindow::realizar_calcs(QVector<QVector<double> > &numbers)
{
   int n1 = 10;
   numbers.resize(n1);
   for(int i = 0;i < n1;i++)
   {
      numbers[i].resize(n2);
   }
}

My difficulty is at startup and the transition to function!

    
asked by anonymous 09.11.2014 / 22:14

1 answer

4

A vector is not meant to behave like an array. Of course there are some solutions.

I found a solution that seems to be getting closer to what you want , see if it helps. It shows how to initialize the array as if it were an array.

There is a Qt class for this but it was considered obsolete and as far as I know nothing was created to replace it. but has a more modern class in version 4.6 onwards.

Another obvious option is to look for a class that will suit you in another library. It may not be as simple to integrate with Qt.

There is still the possibility of creating your class that works as an array. This can bring some integration advantages with Qt depending on how you do it.

I found a gambiarra in this answer in the SO : / p>

int index(int x, int y) {
    return x + width * y;
}

So you simulate an array inside the vector:

QVector<...> vector(width * height);
vector[index(5, 3)] = ...;

I placed GitHub for future reference .

    
09.11.2014 / 22:36