Add space to QGridLayout
-
Hi! I have
QGridLayout
with some buttons, the problem is, it has no space between those buttons: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 theQGridLayout
? Or maybe how to add the scrollbar to be visible withQGridLayout
? Thanks in advance. -
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.
-
Yes, I know about that, but setting
setHorizontalSpacing()
andsetVerticalSpacing()
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. -
Hi,
Something is not clear, why do you put your grid layout in a vbox layout if it's alone ?
-
So, I was right and fixed it by adding
QScrollArea
withQGridLayout
. 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:
The issue is resolved.