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. Grid of QGridLayout not respected

Grid of QGridLayout not respected

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 513 Views 2 Watching
  • 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.
  • S Offline
    S Offline
    Simof
    wrote on last edited by
    #1

    Can you help me to align my widgets at grid?

    2021-06-16 11_00_05-Window.png

    SettingsMenu::SettingsMenu(QWidget *parent) : QFrame(parent)
    {
    
        this->setFrameShape(QFrame::StyledPanel);
        this->setFrameShadow(QFrame::Plain);
    
        QGridLayout* layout = new QGridLayout(this);
    
        makeLanguagesButton();
        QLabel* langLbl = new QLabel("Languages");
        langLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        makeClockButton();
        QLabel* clockLbl = new QLabel("Clock");
        clockLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        makeInfoButton();
        QLabel* infoLbl = new QLabel("Info");
        infoLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        makeCodeButton();
        QLabel* codeLbl = new QLabel("Password");
        codeLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        makeWifiSettingButton();
        QLabel* wifiLbl = new QLabel("WiFi");
        wifiLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        makeNotificationButton();
        QLabel* notificationLbl = new QLabel("Notifications");
        notificationLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    
        QVBoxLayout* langLayout = new QVBoxLayout();
        langLayout->addWidget(languagesBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(langLbl,      0, Qt::AlignCenter);
    
        QVBoxLayout* clockLayout = new QVBoxLayout();
        langLayout->addWidget(clockBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(clockLbl, 0, Qt::AlignCenter);
    
        QVBoxLayout* infoLayout = new QVBoxLayout();
        langLayout->addWidget(infoBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(infoLbl, 0, Qt::AlignCenter);
    
        QVBoxLayout* codeLayout = new QVBoxLayout();
        langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
    
        QVBoxLayout* wifiLayout = new QVBoxLayout();
        langLayout->addWidget(wifiBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(wifiLbl, 0, Qt::AlignCenter);
    
        QVBoxLayout* notificationLayout = new QVBoxLayout();
        langLayout->addWidget(notificationBtn, 0, Qt::AlignCenter);
        langLayout->addWidget(notificationLbl, 0, Qt::AlignCenter);
    
        layout->addLayout(langLayout,          0, 0, Qt::AlignCenter);
        layout->addLayout(clockLayout,         0, 1, Qt::AlignCenter);
        layout->addLayout(infoLayout,          0, 2, Qt::AlignCenter);
        layout->addLayout(codeLayout,          1, 0, Qt::AlignCenter);
        layout->addLayout(wifiLayout,          1, 1, Qt::AlignCenter);
        layout->addLayout(notificationLayout,  1, 2, Qt::AlignCenter);
    
        layout->setContentsMargins(5,5,5,5);
    
        this->setLayout(layout);
    }
    
    Pl45m4P 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      But why all the sub layouts ?

      is the goal just to have them in a grid like ?

      alt text

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Simof
        wrote on last edited by
        #3

        I need the sub layouts because inside every cell of grid I need a pixmap upper and a label below.

        This is the goal

        2021-06-16 11_00_05-Window.png

        1 Reply Last reply
        0
        • S Simof

          Can you help me to align my widgets at grid?

          2021-06-16 11_00_05-Window.png

          SettingsMenu::SettingsMenu(QWidget *parent) : QFrame(parent)
          {
          
              this->setFrameShape(QFrame::StyledPanel);
              this->setFrameShadow(QFrame::Plain);
          
              QGridLayout* layout = new QGridLayout(this);
          
              makeLanguagesButton();
              QLabel* langLbl = new QLabel("Languages");
              langLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
          
              makeClockButton();
              QLabel* clockLbl = new QLabel("Clock");
              clockLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
          
              makeInfoButton();
              QLabel* infoLbl = new QLabel("Info");
              infoLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
          
              makeCodeButton();
              QLabel* codeLbl = new QLabel("Password");
              codeLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
          
              makeWifiSettingButton();
              QLabel* wifiLbl = new QLabel("WiFi");
              wifiLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
          
              makeNotificationButton();
              QLabel* notificationLbl = new QLabel("Notifications");
              notificationLbl->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
          
              QVBoxLayout* langLayout = new QVBoxLayout();
              langLayout->addWidget(languagesBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(langLbl,      0, Qt::AlignCenter);
          
              QVBoxLayout* clockLayout = new QVBoxLayout();
              langLayout->addWidget(clockBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(clockLbl, 0, Qt::AlignCenter);
          
              QVBoxLayout* infoLayout = new QVBoxLayout();
              langLayout->addWidget(infoBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(infoLbl, 0, Qt::AlignCenter);
          
              QVBoxLayout* codeLayout = new QVBoxLayout();
              langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
          
              QVBoxLayout* wifiLayout = new QVBoxLayout();
              langLayout->addWidget(wifiBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(wifiLbl, 0, Qt::AlignCenter);
          
              QVBoxLayout* notificationLayout = new QVBoxLayout();
              langLayout->addWidget(notificationBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(notificationLbl, 0, Qt::AlignCenter);
          
              layout->addLayout(langLayout,          0, 0, Qt::AlignCenter);
              layout->addLayout(clockLayout,         0, 1, Qt::AlignCenter);
              layout->addLayout(infoLayout,          0, 2, Qt::AlignCenter);
              layout->addLayout(codeLayout,          1, 0, Qt::AlignCenter);
              layout->addLayout(wifiLayout,          1, 1, Qt::AlignCenter);
              layout->addLayout(notificationLayout,  1, 2, Qt::AlignCenter);
          
              layout->setContentsMargins(5,5,5,5);
          
              this->setLayout(layout);
          }
          
          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @Simof said in Grid of QGridLayout not respected:

          QVBoxLayout* langLayout = new QVBoxLayout();
          langLayout->addWidget(languagesBtn, 0, Qt::AlignCenter);
          langLayout->addWidget(langLbl, 0, Qt::AlignCenter);

          QVBoxLayout* clockLayout = new QVBoxLayout();
          langLayout->addWidget(clockBtn, 0, Qt::AlignCenter);
          langLayout->addWidget(clockLbl, 0, Qt::AlignCenter);
          
          QVBoxLayout* infoLayout = new QVBoxLayout();
          langLayout->addWidget(infoBtn, 0, Qt::AlignCenter);
          langLayout->addWidget(infoLbl, 0, Qt::AlignCenter);
          
          QVBoxLayout* codeLayout = new QVBoxLayout();
          langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
          langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
          
          QVBoxLayout* wifiLayout = new QVBoxLayout();
          langLayout->addWidget(wifiBtn, 0, Qt::AlignCenter);
          langLayout->addWidget(wifiLbl, 0, Qt::AlignCenter);
          
          QVBoxLayout* notificationLayout = new QVBoxLayout();
          langLayout->addWidget(notificationBtn, 0, Qt::AlignCenter);
          langLayout->addWidget(notificationLbl, 0, Qt::AlignCenter);
          

          If this is your real code, you made a simple mistake :)

          Check your layout names... You add ALL your pairs (pixmap + label) to langLayout, but you create one for each of them. Then you add all to your grid. This results in all your content being your first row/column i.e. langLayout while the other layouts in your grid stay empty

          QVBoxLayout* codeLayout = new QVBoxLayout();
          langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
          langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);

          Replace

          QVBoxLayout* codeLayout = new QVBoxLayout();
          langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
          langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
          

          for example with:

          QVBoxLayout* codeLayout = new QVBoxLayout();
          codeLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
          codeLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
          

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          S 1 Reply Last reply
          2
          • Pl45m4P Pl45m4

            @Simof said in Grid of QGridLayout not respected:

            QVBoxLayout* langLayout = new QVBoxLayout();
            langLayout->addWidget(languagesBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(langLbl, 0, Qt::AlignCenter);

            QVBoxLayout* clockLayout = new QVBoxLayout();
            langLayout->addWidget(clockBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(clockLbl, 0, Qt::AlignCenter);
            
            QVBoxLayout* infoLayout = new QVBoxLayout();
            langLayout->addWidget(infoBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(infoLbl, 0, Qt::AlignCenter);
            
            QVBoxLayout* codeLayout = new QVBoxLayout();
            langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
            
            QVBoxLayout* wifiLayout = new QVBoxLayout();
            langLayout->addWidget(wifiBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(wifiLbl, 0, Qt::AlignCenter);
            
            QVBoxLayout* notificationLayout = new QVBoxLayout();
            langLayout->addWidget(notificationBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(notificationLbl, 0, Qt::AlignCenter);
            

            If this is your real code, you made a simple mistake :)

            Check your layout names... You add ALL your pairs (pixmap + label) to langLayout, but you create one for each of them. Then you add all to your grid. This results in all your content being your first row/column i.e. langLayout while the other layouts in your grid stay empty

            QVBoxLayout* codeLayout = new QVBoxLayout();
            langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);

            Replace

            QVBoxLayout* codeLayout = new QVBoxLayout();
            langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
            

            for example with:

            QVBoxLayout* codeLayout = new QVBoxLayout();
            codeLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
            codeLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
            
            S Offline
            S Offline
            Simof
            wrote on last edited by
            #5

            @Pl45m4 said in Grid of QGridLayout not respected:

            @Simof said in Grid of QGridLayout not respected:

            QVBoxLayout* langLayout = new QVBoxLayout();
            langLayout->addWidget(languagesBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(langLbl, 0, Qt::AlignCenter);

            QVBoxLayout* clockLayout = new QVBoxLayout();
            langLayout->addWidget(clockBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(clockLbl, 0, Qt::AlignCenter);
            
            QVBoxLayout* infoLayout = new QVBoxLayout();
            langLayout->addWidget(infoBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(infoLbl, 0, Qt::AlignCenter);
            
            QVBoxLayout* codeLayout = new QVBoxLayout();
            langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
            
            QVBoxLayout* wifiLayout = new QVBoxLayout();
            langLayout->addWidget(wifiBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(wifiLbl, 0, Qt::AlignCenter);
            
            QVBoxLayout* notificationLayout = new QVBoxLayout();
            langLayout->addWidget(notificationBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(notificationLbl, 0, Qt::AlignCenter);
            

            If this is your real code, you made a simple mistake :)

            Check your layout names... You add ALL your pairs (pixmap + label) to langLayout, but you create one for each of them. Then you add all to your grid. This results in all your content being your first row/column i.e. langLayout while the other layouts in your grid stay empty

            QVBoxLayout* codeLayout = new QVBoxLayout();
            langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);

            Replace

            QVBoxLayout* codeLayout = new QVBoxLayout();
            langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
            langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
            

            for example with:

            QVBoxLayout* codeLayout = new QVBoxLayout();
            codeLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
            codeLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
            

            Yes this is my real code! Thank a lot, I lose a lot of time and it was a very stupid mistake.

            Pl45m4P 1 Reply Last reply
            2
            • S Simof

              @Pl45m4 said in Grid of QGridLayout not respected:

              @Simof said in Grid of QGridLayout not respected:

              QVBoxLayout* langLayout = new QVBoxLayout();
              langLayout->addWidget(languagesBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(langLbl, 0, Qt::AlignCenter);

              QVBoxLayout* clockLayout = new QVBoxLayout();
              langLayout->addWidget(clockBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(clockLbl, 0, Qt::AlignCenter);
              
              QVBoxLayout* infoLayout = new QVBoxLayout();
              langLayout->addWidget(infoBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(infoLbl, 0, Qt::AlignCenter);
              
              QVBoxLayout* codeLayout = new QVBoxLayout();
              langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
              
              QVBoxLayout* wifiLayout = new QVBoxLayout();
              langLayout->addWidget(wifiBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(wifiLbl, 0, Qt::AlignCenter);
              
              QVBoxLayout* notificationLayout = new QVBoxLayout();
              langLayout->addWidget(notificationBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(notificationLbl, 0, Qt::AlignCenter);
              

              If this is your real code, you made a simple mistake :)

              Check your layout names... You add ALL your pairs (pixmap + label) to langLayout, but you create one for each of them. Then you add all to your grid. This results in all your content being your first row/column i.e. langLayout while the other layouts in your grid stay empty

              QVBoxLayout* codeLayout = new QVBoxLayout();
              langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);

              Replace

              QVBoxLayout* codeLayout = new QVBoxLayout();
              langLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
              langLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
              

              for example with:

              QVBoxLayout* codeLayout = new QVBoxLayout();
              codeLayout->addWidget(codeBtn, 0, Qt::AlignCenter);
              codeLayout->addWidget(codeLbl, 0, Qt::AlignCenter);
              

              Yes this is my real code! Thank a lot, I lose a lot of time and it was a very stupid mistake.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @Simof said in Grid of QGridLayout not respected:

              I lose a lot of time and it was a very stupid mistake.

              This happens very often if you copy & paste lines of code, because you need it multiple times. Then, when you forget to change the var names correctly you run into stuff like this. And most of the time you don't know what and why this happend :)


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              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