Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. 2D QVector of QLabel
Qt 6.11 is out! See what's new in the release blog

2D QVector of QLabel

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 430 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt-User0307
    wrote on last edited by
    #1

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

    J.HilkJ 1 Reply Last reply
    0
    • Q Qt-User0307

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

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • Q Offline
        Q Offline
        Qt-User0307
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved