PDA

View Full Version : QPushButtons not stretching in QGridLayout



rawfool
8th April 2013, 11:43
I need the widgets in my gridlayout to be stretched so that it'll occupy the available space without any gaps. But I could'nt get that working.

I have a QMainWindow, on which I have QStackedWidget as centralWidget. I have added a page (QWidget) to QStackedWidget and set QGridLayout to the page.
Now I've added 3 QPushButtons in the top row of QGridLayout and 2 QPushButtons in the next row. I'm expecting the QPushButtons stretching across the layout both interms of horizontal & vertical spacing. Buthe buttons are of small size leaving large gaps in the layout. I tried the stretching, sizePolicy, but in vain. Please help me fix the issue.
Thank you.


lyt->addWidget(m_pbVulnerability, 0, 0, 2, 3, Qt::AlignCenter);
lyt->addWidget(m_pbPatch, 0, 3, 2, 3, Qt::AlignCenter);
lyt->addWidget(m_pbConfig, 0, 6, 2, 3, Qt::AlignCenter);
lyt->addWidget(m_pbScan, 1, 3, 2, 6, Qt::AlignCenter);
lyt->addWidget(m_scanTimeSelect, 5, 3, 2, 3, Qt::AlignCenter);
lyt->addWidget(m_pbUpdate, 5, 6, 2, 3, Qt::AlignCenter);

giblit
8th April 2013, 18:25
Pushbutton->setMaximumSize() or maxHeight and maxWidth the thing u did wad make it so if its long then it will go into multiple grid spots but first u must make it long

rawfool
15th April 2013, 12:46
Hi giblit,

I don't want to set any size to the buttons. As I'm using QGridLayout, I want to make use of layout properties for auto sizing of buttons. So any hint of where I'm doing wrong ?


m_Button1 = new QPushButton("Button1", this);
m_Button1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

m_Button2 = new QPushButton("Button2", this);
m_Button2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

m_Button3 = new QPushButton("Button3", this);
m_Button3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

m_Button4 = new QPushButton("Button4");
m_Button4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

m_Button5 = new QPushButton("Update");
m_Button5->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

m_scanTimeSelect = new QSlider(Qt::Horizontal);
m_scanTimeSelect->setTickInterval(12);
m_scanTimeSelect->setTickPosition(QSlider::TicksBelow);
m_scanTimeSelect->setStyleSheet("QSlider::groove:horizontal {border: 1px solid #bbb;background: white;height: 10px;border-radius: 4px;}"
"QSlider::sub-page:horizontal {background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #66e, stop: 1 #bbf); background: qlineargradient(x1: 0, y1: 0.2, x2: 1, y2: 1, stop: 0 #bbf, stop: 1 #55f); border: 1px solid #777; height: 10px; border-radius: 4px; }"
"QSlider::add-page:horizontal { background: #fff; border: 1px solid #777; height: 10px; border-radius: 4px; }"
"QSlider::handle:horizontal {background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #eee, stop:1 #ccc); border: 1px solid #777; width: 13px; margin-top: -2px; margin-bottom: -2px; border-radius: 4px; }"
"QSlider::handle:horizontal:hover { background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #fff, stop:1 #ddd); border: 1px solid #444; border-radius: 4px; }"
"QSlider::sub-page:horizontal:disabled { background: #bbb; border-color: #999; }"
"QSlider::add-page:horizontal:disabled { background: #eee; border-color: #999; }"
"QSlider::handle:horizontal:disabled { background: #eee; border: 1px solid #aaa; border-radius: 4px; }");

lyt->addWidget(m_Button1, 0, 0, 2, 3, Qt::AlignCenter);
lyt->addWidget(m_Button2, 0, 3, 2, 3, Qt::AlignCenter);
lyt->addWidget(m_Button3, 0, 6, 2, 3, Qt::AlignCenter);
lyt->addWidget(m_Button4, 3, 3, 2, 6, Qt::AlignCenter);
lyt->addWidget(m_scanTimeSelect, 5, 3, 2, 6, Qt::AlignCenter);
lyt->addWidget(m_Button5, 5, 6, 2, 3, Qt::AlignCenter);

Kindly help me tackle this problem. Thank you.

pradeepreddyg95
15th April 2013, 13:29
hi rawfool, add below lines i hope it shoud work.

QPushButton *pButn = new QPushButton(this);

QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(pButn->sizePolicy().hasHeightForWidth());
pButn->setSizePolicy(sizePolicy);

QPushButton *pButn1 = new QPushButton(this);
pButn1->setSizePolicy(sizePolicy);

QPushButton *pButn2 = new QPushButton(this);
pButn2->setSizePolicy(sizePolicy);

QPushButton *pButn3 = new QPushButton(this);
pButn3->setSizePolicy(sizePolicy);

lyt->addWidget(pButn, 0, 0, 2, 3);
lyt->addWidget(pButn1, 0, 3, 2, 3);
lyt->addWidget(pButn2, 0, 6, 2, 3);
lyt->addWidget(pButn3, 3, 3, 2, 6);

Santosh Reddy
15th April 2013, 13:56
Don't use Qt::AlignCenter while adding the widgets, this will force the layout to use the minimum/preferable size and pad the rest of area.

rawfool
15th April 2013, 14:14
@ Pradeep - It worked! Thanks for the trick.
@Santosh - Thanks for the insight.