I'm trying to set the layout of a widget manually through code (not in Designer), but I'm doing something wrong, because I get this warning:
QWidget::setLayout: Attempting to set QLayout "" on Widget "", which already has a layout
And also the layout is messed up (the label is at the top, instead of the bottom).
This is an example code that reproduces the problem:
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
QLabel *label = new QLabel("Test", this);
QHBoxLayout *hlayout = new QHBoxLayout(this);
QVBoxLayout *vlayout = new QVBoxLayout(this);
QSpacerItem *spacer = new QSpacerItem(40, 20, QSizePolicy::Fixed);
QLineEdit *lineEdit = new QLineEdit(this);
hlayout->addItem(spacer);
hlayout->addWidget(lineEdit);
vlayout->addLayout(hlayout);
vlayout->addWidget(label);
setLayout(vlayout);
}
So I believe your problem is in this line:
In particular, I think the problem is passing
this
into theQHBoxLayout
. Because you intend for thisQHBoxLayout
to NOT be the top level layout ofthis
, you should not passthis
into the constructor.Here's my re-write that I hacked into a test app locally and seems to work great:
The problem is that you are creating layouts with a parent of
this
. When you do that, it sets the layout to be the main layout ofthis
. Thus, it is redundant to callsetMainLayout()
.