Question

Following simplified code snippet:

#include <QtGui>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
QFormLayout *form = new QFormLayout();

// first row
form->addRow("First row:", new QLineEdit());

// second row
QWidget *hbox_widget = new QWidget();
QBoxLayout *hbox = new QHBoxLayout();
hbox->addWidget(new QLineEdit());
hbox_widget->setLayout(hbox);
form->addRow("Second row:", hbox_widget);

window->setLayout(form);
window->show();
return app.exec();
}

Here, I create a QFormLayout with two rows: the first is a classic example of label+input widget, the second one is almost the same, with the exception that the input widget is "boxed" in a QHBoxLayout(+containing widget).

result of above code

For some reason, this seems to screw up the "vertical centering" of the input widget (in the 2nd row). How can I add a widget which contains others in a hboxlayout to a formlayout, and still have them all centered vertically?

Was it helpful?

Solution

Set the contents margins of the QHBoxLayout to 0:

QBoxLayout *hbox = new QHBoxLayout();
hbox->setContentsMargins(0, 0, 0, 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top