vitaR
vitaR

Reputation: 23

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.

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:

<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:

    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? How to show in the Main Window? I need a 2D Widget too? Thanks for your time.

Upvotes: 1

Views: 2400

Answers (1)

vahancho
vahancho

Reputation: 21240

Assuming that the x and y are number of rows and columns, correspondingly, you can simply do this trick:

[..]
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);
    }
}
[..]

Upvotes: 2

Related Questions