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. Add space to QGridLayout

Add space to QGridLayout

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 8.2k 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    Hi! I have QGridLayout with some buttons, the problem is, it has no space between those buttons:

    0_1557408768844_2019-05-09_163156.png

    Every button has the setMinimumHeight(23); min height set to 23 pixels.

    Code:

    QGridLayout *gridLayout1 = new QGridLayout();
    gridLayout1->setContentsMargins(0, 5, 10, 5);
    gridLayout1->setAlignment(Qt::AlignTop);
    gridLayout1->addWidget(button1, 1, 0);
    gridLayout1->addWidget(button2, 1, 1);
    
    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setContentsMargins(10, 0, 0, 0);
    mainLayout->setAlignment(Qt::AlignTop);
    mainLayout->addLayout(gridLayout1);
    

    I have tried setting setHorizontalSpacing, setVerticalSpacing, setMargin but the problem still exists. Any ideas how to add space between those buttons to the QGridLayout? Or maybe how to add the scrollbar to be visible with QGridLayout? Thanks in advance.

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      QLayout::setSpacing(int) -- sets spacing between widgets in layout

      For QGridLayout and QFormLayout, it is possible to set different horizontal and vertical spacings using setHorizontalSpacing() and setVerticalSpacing(). In that case, spacing() returns -1.

      If you meet the AI on the road, kill it.

      Cobra91151C 1 Reply Last reply
      3
      • Kent-DorfmanK Kent-Dorfman

        QLayout::setSpacing(int) -- sets spacing between widgets in layout

        For QGridLayout and QFormLayout, it is possible to set different horizontal and vertical spacings using setHorizontalSpacing() and setVerticalSpacing(). In that case, spacing() returns -1.

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #3

        @Kent-Dorfman

        Yes, I know about that, but setting setHorizontalSpacing() and setVerticalSpacing() in my case does nothing. Only when resize the app window to a bigger height then it displays the space between those buttons. But resizing is not the option here. Thanks.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Something is not clear, why do you put your grid layout in a vbox layout if it's alone ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Cobra91151C 2 Replies Last reply
          0
          • SGaistS SGaist

            Hi,

            Something is not clear, why do you put your grid layout in a vbox layout if it's alone ?

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by Cobra91151
            #5

            @SGaist

            Hi! I use QVBoxLayout because it contains the QLabel's which adds the sections for those buttons.

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Something is not clear, why do you put your grid layout in a vbox layout if it's alone ?

              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by
              #6

              @SGaist

              I think that I should try to use QScrollArea with QGridLayout to fix this issue. I will try it and reply tomorrow.

              1 Reply Last reply
              0
              • Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by Cobra91151
                #7

                So, I was right and fixed it by adding QScrollArea with QGridLayout. I created the test example:

                Code:

                    QPushButton *testButton1 = new QPushButton("Test1");
                    testButton1->setMinimumHeight(23);
                    QPushButton *testButton2 = new QPushButton("Test2");
                    testButton2->setMinimumHeight(23);
                    QPushButton *testButton3 = new QPushButton("Test");
                    testButton3->setMinimumHeight(23);
                    QPushButton *testButton4 = new QPushButton("Test");
                    testButton4->setMinimumHeight(23);
                    QPushButton *testButton5 = new QPushButton("Test");
                    testButton5->setMinimumHeight(23);
                    QPushButton *testButton6 = new QPushButton("Test");
                    testButton6->setMinimumHeight(23);
                    QPushButton *testButton7 = new QPushButton("Test");
                    testButton7->setMinimumHeight(23);
                    QPushButton *testButton8 = new QPushButton("Test");
                    testButton8->setMinimumHeight(23);
                    QPushButton *testButton9 = new QPushButton("Test");
                    testButton9->setMinimumHeight(23);
                    QPushButton *testButton10 = new QPushButton("Test");
                    testButton10->setMinimumHeight(23);
                    QPushButton *testButton11 = new QPushButton("Test");
                    testButton11->setMinimumHeight(23);
                    QPushButton *testButton12 = new QPushButton("Test");
                    testButton12->setMinimumHeight(23);
                    QPushButton *testButton13 = new QPushButton("Test");
                    testButton13->setMinimumHeight(23);
                    QPushButton *testButton14 = new QPushButton("Test");
                    testButton14->setMinimumHeight(23);
                    QPushButton *testButton15 = new QPushButton("Test");
                    testButton15->setMinimumHeight(23);
                    QPushButton *testButton16 = new QPushButton("Test");
                    testButton16->setMinimumHeight(23);
                
                    QFont sectionFont;
                    sectionFont.setBold(true);
                    QLabel *section1Label = new QLabel("Section 1", this);
                    section1Label->setFont(sectionFont);
                    QLabel *section2Label = new QLabel("Section 2", this);
                    section2Label->setFont(sectionFont);
                
                    QWidget *testWidget = new QWidget(this);
                    QScrollArea *testScrollArea = new QScrollArea(this);
                    testScrollArea->setWidgetResizable(true);
                    testScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
                    testScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
                
                    QGridLayout *testGridLayout = new QGridLayout();
                    testGridLayout->addWidget(testButton1, 1, 0);
                    testGridLayout->addWidget(testButton2, 1, 1);
                    testGridLayout->addWidget(testButton3, 1, 2);
                    testGridLayout->addWidget(testButton4, 1, 3);
                    testGridLayout->addWidget(testButton5, 1, 4);
                    testGridLayout->addWidget(section1Label, 2, 0);
                    testGridLayout->addWidget(testButton6, 3, 0);
                    testGridLayout->addWidget(testButton7, 3, 1);
                    testGridLayout->addWidget(testButton8, 3, 2);
                    testGridLayout->addWidget(testButton9, 3, 3);
                    testGridLayout->addWidget(testButton10, 3, 4);
                    testGridLayout->addWidget(section2Label, 4, 0);
                    testGridLayout->addWidget(testButton11, 5, 0);
                    testGridLayout->addWidget(testButton12, 5, 1);
                    testGridLayout->addWidget(testButton13, 5, 2);
                    testGridLayout->addWidget(testButton14, 5, 3);
                    testGridLayout->addWidget(testButton15, 5, 4);
                    testGridLayout->addWidget(testButton16, 5, 0);
                    testWidget->setLayout(testGridLayout);
                    testScrollArea->setWidget(testWidget);
                
                    QHBoxLayout *mainHBoxLayout = new QHBoxLayout();
                    mainHBoxLayout->addWidget(testScrollArea);
                    setLayout(mainHBoxLayout);
                

                Now it adds the scrollbars and the space between those buttons:

                0_1557483836424_2019-05-10_132343.png

                The issue is resolved.

                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