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. GridLayout: Components getting pushed to the next row
QtWS25 Last Chance

GridLayout: Components getting pushed to the next row

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 1.2k 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.
  • S Offline
    S Offline
    Smeeth
    wrote on last edited by
    #1

    I am building a dialog to enter a first name, last name, height, weight, and buttons to convert the height and weight between units.

    The first row with the names displays fine, and the first component of the second row displays correctly as well.
    0_1530124634070_f1270fd3-4ddb-49a4-8ab5-68fe44ce67d5-image.png

    However, when I add the second part of the second row (what would be coordinate (1,1) in the Grid), it does not get displayed correctly:
    0_1530124733666_65f70da2-ca5c-4f4d-9c6b-7d0f379ff2e7-image.png

    Here is the .cpp for my dialog:

    #include "patienteditorpanel.h"
    #include <QGridLayout>
    #include <QLabel>
    #include <QGuiApplication>
    #include <QSize>
    #include <QScreen>
    #include <QPushButton>
    
    MyDialog::MyDialog(QWidget *parent) : QDialog(parent)
    {
        this->setStyleSheet(
                    "MyDialog {background-color: Gainsboro;}"
                    "QLabel {font: 16px 'Tahoma'; color: blue;}"
                    "QLineEdit {font: 24px 'Tahoma'; color: blue;}"
                    "QPushButton {font: 16px 'Tahoma'; color: blue;}"
                    );
    
        this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
        this->setModal(true);
    
        QLabel *firstNameLbl = new QLabel("First Name: ");
        firstNameTxt = new QLineEdit();
    
        QLabel *lastNameLbl = new QLabel("Last Name: ");
        lastNameTxt = new QLineEdit();
    
        QLabel *heightLbl = new QLabel("Height: ");
        heightTxt = new QLineEdit();
    
        heightConvertBtn = new QPushButton("Convert\nHeight");
    
        QLabel *weightLbl = new QLabel("Weight: ");
        weightTxt = new QLineEdit();
    
        weightConvertBtn = new QPushButton("Convert\nWeight");
    
        QVBoxLayout *firstNamePnl = new QVBoxLayout;
        firstNamePnl->addWidget(firstNameLbl);
        firstNamePnl->addWidget(firstNameTxt);
    
        QVBoxLayout *lastNamePnl = new QVBoxLayout;
        lastNamePnl->addWidget(lastNameLbl);
        lastNamePnl->addWidget(lastNameTxt);
    
        QVBoxLayout *heightTxtPnl = new QVBoxLayout;
        heightTxtPnl->addWidget(heightLbl);
        heightTxtPnl->addWidget(heightTxt);
    
        QHBoxLayout *heightPnl = new QHBoxLayout;
        heightPnl->addLayout(heightTxtPnl);
        heightPnl->addWidget(heightConvertBtn);
    
        QVBoxLayout *weightTxtPnl = new QVBoxLayout;
        heightTxtPnl->addWidget(weightLbl);
        heightTxtPnl->addWidget(weightTxt);
    
        QHBoxLayout *weightPnl = new QHBoxLayout;
        heightPnl->addLayout(weightTxtPnl);
        heightPnl->addWidget(weightConvertBtn);
    
        QGridLayout *mainLayout = new QGridLayout();
        mainLayout->addLayout(firstNamePnl, 0, 0);
        mainLayout->addLayout(lastNamePnl, 0, 1);
        mainLayout->addLayout(heightPnl, 1, 0);
        mainLayout->addLayout(weightPnl, 1, 1);
    
        this->setLayout(mainLayout);
    }
    
    

    And here is the header:

    #include <QTextEdit>
    
    class MyDialog: public QDialog
    {
        Q_OBJECT
    public:
        explicit MyDialog(QWidget *parent = nullptr);
    
    signals:
    
    public slots:
    
    private:
        QLineEdit *mrnTxt;
        QLineEdit *firstNameTxt, *lastNameTxt;
        QLineEdit *heightTxt, *weightTxt;
        QPushButton *heightConvertBtn, *weightConvertBtn;
    
    };
    
    #endif // MYDIALOG_H
    

    As you can see I am using a combination of QHBoxLayout and QVBoxLayout to build my form. Is this why I am running into trouble?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      I wondering if its just the default layout margins that make it seems not
      aligned.
      I assume the issue is the Convert buttons not being aligned or ?

      S 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        I wondering if its just the default layout margins that make it seems not
        aligned.
        I assume the issue is the Convert buttons not being aligned or ?

        S Offline
        S Offline
        Smeeth
        wrote on last edited by
        #3

        @mrjj I would like the weightLbl, weightTxt, and weightConvertBtn to appear underneath the Last Name block. I would assume this would be coordinate (1,1) in the grid.

        Here's what I'm going for (excuse my crude Paint creation)

        0_1530126963702_230fa26d-c46f-4fa1-9efa-0970edf60b8e-image.png

        mrjjM 1 Reply Last reply
        0
        • S Smeeth

          @mrjj I would like the weightLbl, weightTxt, and weightConvertBtn to appear underneath the Last Name block. I would assume this would be coordinate (1,1) in the grid.

          Here's what I'm going for (excuse my crude Paint creation)

          0_1530126963702_230fa26d-c46f-4fa1-9efa-0970edf60b8e-image.png

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Smeeth
          Your paint mockup is super :)
          Well its not the box layout in it self.
          alt text

          It should not a issue mixing them but they might take some extra space than widgets alone.
          Since they are not visible its hard to say.
          Can code run as is ? ( if i paste it to a default project and create MyDialog ?)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Smeeth
            wrote on last edited by
            #5

            Another question, unrelated to this one, is why are widgets that I create but never add to a layout displayed on the screen? If I remove the line mainLayout->addLayout(weightPnl, 1, 1); the weight widgets still get added to the screen. Does simply calling the constructor for a control automatically add it to its parent's form?

            mrjjM 1 Reply Last reply
            0
            • S Smeeth

              Another question, unrelated to this one, is why are widgets that I create but never add to a layout displayed on the screen? If I remove the line mainLayout->addLayout(weightPnl, 1, 1); the weight widgets still get added to the screen. Does simply calling the constructor for a control automatically add it to its parent's form?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Smeeth
              If you give it a parent when you create them they are inserted into the widget. Widgets can hold
              other widgets even when no layout.
              So in short. yes creating them does show on screen in most cases.

              1 Reply Last reply
              1
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                Btw i dont know if it helps but this code generate the layout

                  auto widget = centralWidget();
                  auto gridLayout = new QGridLayout(widget); // widget is the top parent.  often centralwidget
                  auto     label = new QLabel(widget);
                  gridLayout->addWidget(label, 0, 0, 1, 1);
                
                  auto label_2 = new QLabel(widget);
                  gridLayout->addWidget(label_2, 0, 2, 1, 1);
                
                  auto label_4 = new QLabel(widget);
                  gridLayout->addWidget(label_4, 2, 2, 1, 1);
                
                  auto label_3 = new QLabel(widget);
                  gridLayout->addWidget(label_3, 2, 0, 1, 1);
                
                  auto lineEdit_5 = new QLineEdit(widget);
                  gridLayout->addWidget(lineEdit_5, 3, 2, 1, 1);
                
                  auto lineEdit_3 = new QLineEdit(widget);
                  gridLayout->addWidget(lineEdit_3, 3, 0, 1, 1);
                
                  auto lineEdit = new QLineEdit(widget);
                  gridLayout->addWidget(lineEdit, 1, 0, 1, 2);
                
                  auto pushButton = new QPushButton(widget);
                  gridLayout->addWidget(pushButton, 3, 1, 1, 1);
                
                  auto pushButton_2 = new QPushButton(widget);
                  gridLayout->addWidget(pushButton_2, 3, 3, 1, 1);
                
                  auto lineEdit_2 = new QLineEdit(widget);
                  gridLayout->addWidget(lineEdit_2, 1, 2, 1, 2);
                
                1 Reply Last reply
                2
                • S Offline
                  S Offline
                  Smeeth
                  wrote on last edited by
                  #8

                  Thank you for the sample code. I thought it might have something to do with too many layouts and their margins interfering and was going to trying a GridLayout similar to this. Not sure exactly what the problem is, but a GridLayout is probably a simpler, and less confusing way to do this. Thanks.

                  mrjjM 1 Reply Last reply
                  0
                  • S Smeeth

                    Thank you for the sample code. I thought it might have something to do with too many layouts and their margins interfering and was going to trying a GridLayout similar to this. Not sure exactly what the problem is, but a GridLayout is probably a simpler, and less confusing way to do this. Thanks.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #9

                    @Smeeth
                    Well im sure your other method can work but its hard to see why its not
                    aligned as we think.
                    If Grid layout is looking like you want, then yes. Should be fine.
                    Took 1 minute to make in Designer.
                    I tested sample. it does produce the layout. But im not setting texts so it looks odd :)
                    alt text
                    but labels are there
                    alt text

                    1 Reply Last reply
                    0

                    • Login

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