我正在尝试通过代码手动设置小部件的布局(而不是在设计师中设置),但我做错了什么,因为我收到以下警告:
QWidget::setLayout: 尝试在已经有布局的小部件“”上设置QLayout“”,
并且布局混乱了(标签在顶部而不是底部)。
这是一个可以复现问题的示例代码:
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);
}
QHBoxLayout *buttonLayout = new QHBoxLayout();
而不是:QHBoxLayout *buttonLayout = new QHBoxLayout(this);
- user1369511