2D QVector of QLabel
-
Hello,
i have created the following 2D QVector of QLabels and i wanted to set a Pixmap on all of the labels. My goal is to have a 5x5 field filled with Pixmaps.QVector<QVector<QLabel*>> labels { {0, 0, 0, 0 ,0}, {0, 0, 0, 0 ,0}, {0, 0, 0, 0 ,0}, {0, 0, 0, 0 ,0}, {0, 0, 0, 0 ,0} };The MainWindow code looks like this:
ui->setupUi(this); this->setWindowTitle("Lawnmower"); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { labels[i][j]->setPixmap(NotMowed); labels[i][j]->setGeometry(labelPosX, labelPosY, 200, 100); labelPosX += 172; if (i == 0 || i == 1 || i == 2 || i == 3 || i == 4) { labelPosX = 0; labelPosY += 100; } } }Dont mind the Variables labelPosX and labelPosY i just made them for setting the size and position.
Anyways suddenly when i try to start it i get a segmentation fault. Can anyone help me? Thanks for your response. :) -
Hello,
i have created the following 2D QVector of QLabels and i wanted to set a Pixmap on all of the labels. My goal is to have a 5x5 field filled with Pixmaps.QVector<QVector<QLabel*>> labels { {0, 0, 0, 0 ,0}, {0, 0, 0, 0 ,0}, {0, 0, 0, 0 ,0}, {0, 0, 0, 0 ,0}, {0, 0, 0, 0 ,0} };The MainWindow code looks like this:
ui->setupUi(this); this->setWindowTitle("Lawnmower"); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { labels[i][j]->setPixmap(NotMowed); labels[i][j]->setGeometry(labelPosX, labelPosY, 200, 100); labelPosX += 172; if (i == 0 || i == 1 || i == 2 || i == 3 || i == 4) { labelPosX = 0; labelPosY += 100; } } }Dont mind the Variables labelPosX and labelPosY i just made them for setting the size and position.
Anyways suddenly when i try to start it i get a segmentation fault. Can anyone help me? Thanks for your response. :)@Qt-User0307 did you ever initialise the 25 QLabels ? I don't see that anywhere from what you've shown here.
Besides that, use a Debugger
-
Yes thanks for your quick answer!
And yea unfortunately i forgot to initialise the labels, so i added this code:for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) labels[i][j] = new QLabel(this);Just in case of other people having the same mistake and not knowing what they did wrong/forgot.