PDA

View Full Version : QHBoxLayout widget spacing



K4ELO
6th March 2013, 19:17
Can't seem to get widgets in a layout spaced the way I want.


// panel_1 class
int w = 50;
int h = 100;

pbStart = new QPushButton("Start");
QHBoxLayout *layout_1 = new QHBoxLayout(this);
layout_1->setSpacing(0);

layout_1->addWidget(pbStart);

lineEdit1 = new QLineEdit(this);
lineEdit2 = new QLineEdit(this);
lineEdit3 = new QLineEdit(this);

lineEdit1->setFixedSize(w, h);
lineEdit2->setFixedSize(w, h);
lineEdit3->setFixedSize(w, h);

layout_1->addWidget(lineEdit1);
layout_1->addWidget(lineEdit2);
layout_1->addWidget(lineEdit3);

// mainwindow class
QWidget *widget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout;
layout->setSpacing(1);

panel_1 = new Panel_1();
layout->addWidget(panel_1);
layout->addWidget(d_.view);

widget->setLayout(layout);
setCentralWidget(widget);

The space between the pb and the line edits and between the edits is constant - about 2"
Checking spacing() with debug, it reports 0.

What am I doing wrong that's not letting me set the spacing to what I need?
Thanks for any help.

ChrisW67
7th March 2013, 07:12
I guess you hou have four fixed width widgets in a horizontal layout that is wider than the four widgets so the layout engine distributes the spare space evenly. Try adding the widgets to the layout with the alignment option of addWidget() specified.

K4ELO
7th March 2013, 14:47
Thanks Chris, using the alignment option when adding the widget solves the problem.