c++arraysqtqvectorqvector3d

How to declare and use in Qt a 2D integer array (GUI)?


I wanted to create a 2D integer array in the header file of my QT GUI APPLICATION with 2 columns and a yet to be defined (hence dynamic) number of rows.

So far I've got this: to make a dynamic array

QVector <qint8> ArrayName;

Can I use it as a 2D array or not? And, how would I call a certain row in a certain column later? e.g. ArrayName[40][2] ?


Solution

  • One could create a QVector<QVector<qint8>>, but I would rather not go there: It’s unwieldy and not very efficient. I’d just fold the dimensions into a one-dimensional array:

    const int NUMBER_OF_COLUMNS = 2;
    QVector<qint8> data;
    ...
    data.resize(numberOfRows * NUMBER_OF_COLUMNS);
    ...
    // Get (row, column):
    const qint8 v = data[row*2+column]; // column being 0 or 1