简体   繁体   English

如何在Qt中声明和使用2D整数数组(GUI)?

[英]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. 我想在我的QT GUI APPLICATION的头文件中创建一个2D整数数组,其中包含2列和尚未定义(因此动态)的行数。

So far I've got this: to make a dynamic array 到目前为止,我已经知道了:创建一个动态数组

QVector <qint8> ArrayName;

Can I use it as a 2D array or not? 是否可以将其用作2D阵列? And, how would I call a certain row in a certain column later? 而且,以后如何在特定列中调用特定行? eg ArrayName[40][2] ? 例如ArrayName[40][2]

One could create a QVector<QVector<qint8>> , but I would rather not go there: It's unwieldy and not very efficient. 可以创建一个QVector<QVector<qint8>> ,但我宁愿不去那里:它笨拙且效率不高。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM