简体   繁体   English

如何在QT中制作二维数组?

[英]How to make a 2d Array in QT?

Hi my question is simple, I just asked in the QT Forums but nobody answer to me. 嗨,我的问题很简单,我只是在QT论坛中问过,但没有人回答我。

I just wanted to make a 2D array of a QLabel, can somebody help me, All I read about it, they use a dynamic vector, something like this: 我只想制作一个QLabel的2D数组,有人可以帮我吗,我所读到的所有内容,它们都使用动态矢量,如下所示:

<QVector <Data_Type>> 

I can't use that (My project don't have to use that yet, crap specifications I know), so I have to use a 2D like in C++ or C. EDIT: I have the 2D Array but dont know how to show it, All I have is this, and don't give me errors: 我不能使用它(我的项目不必使用它,我知道废话规范),所以我必须像在C ++或C中那样使用2D。编辑:我有2D数组,但不知道如何显示它,我所拥有的就是这个,不要给我错误:

    QWidget *mainWidget = new QWidget;
    QLabel **maze;
    maze= new QLabel*[x];
    for (int i = 0; i < x; i++) {
        maze[i]= new QLabel[y];
    }
    for(int i=0;i<x;i++){
        for(int j=0;j<y;j++){
            maze[i][j].setPixmap(test);
            maze[i][j].move(i*60,j*60);
        }
    }
    mainWidget->show();
           setCentralWidget(mainWidget);

Now I just want to show the images, once I run the project, no images appear, is the Widget thing right? 现在,我只想显示图像,一旦运行项目,就不会出现图像,是Widget吗? How to show in the Main Window? 如何在主窗口中显示? I need a 2D Widget too? 我也需要2D小部件吗? Thanks for your time. 谢谢你的时间。

Assuming that the x and y are number of rows and columns, correspondingly, you can simply do this trick: 假设x和y分别是行数和列数,则可以简单地执行以下操作:

[..]
QGridLayout *grid = new QGridLayout;
for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
        QLabel *label = new QLabel(this);
        label->setPixmap("Path_Of_The_Image");
        grid.addWidget(label, i, j);
    }
}
[..]

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

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