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

1.1k views Asked by At

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] ?

1

There are 1 answers

0
Frank Osterfeld On BEST ANSWER

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