自定义对话框的布局

问题描述 投票:0回答:1

在我的 Qt 桌面应用程序中,我需要一个对话框供用户输入一些数据。 所以我选择:新文件| Qt |设计器表单类,然后是底部带有按钮的对话框,并适当命名。

在新对话框的构造函数中,我创建一些标签和行编辑并将它们添加到网格布局中。 像这样:

ui->setupUi(this);

partNoLabel = new QLabel(tr("&Part No:"));
partDescLabel = new QLabel(tr("&Description:"));

partNoLineEdit = new QLineEdit();
partDescLineEdit = new QLineEdit();

partNoLabel->setBuddy(partNoLineEdit);
partDescLabel->setBuddy(partDescLineEdit);

supplierLabel = new QLabel(tr("Supplier"));
supPartNoLabel = new QLabel(tr("Part No"));
supplierLineEdit = new QLineEdit();
supPartNoLineEdit = new QLineEdit();

supplierLabel->setBuddy(supplierLineEdit);
supPartNoLabel->setBuddy(supPartNoLineEdit);


QGridLayout *dlgLayout = new QGridLayout;
dlgLayout->addWidget(partNoLabel, 0, 0);
dlgLayout->addWidget(partNoLineEdit, 0, 1);
dlgLayout->addWidget(partDescLabel, 1, 0);
dlgLayout->addWidget(partDescLineEdit, 1, 1);

dlgLayout->addWidget(supplierLabel, 2, 0);
dlgLayout->addWidget(supplierLineEdit, 2, 1);
dlgLayout->addWidget(supPartNoLabel, 3, 0);
dlgLayout->addWidget(supPartNoLineEdit, 3, 1);

setLayout(dlgLayout);

setWindowTitle("New part");

但是,当显示时 - 它看起来很糟糕:

dialog showing buttons over edit boxes

调整大小后,看起来更糟:

resized dialog showing buttons over edit boxes

这显然是不可接受的,我该如何解决这个问题,或者我应该如何做到这一点?

固定版本:

我遵循 Gábor Angyal 的建议并手动完成,在代码中删除并添加了一个按钮框。 我还更改为使用 FormLayout,如本文所述:http://doc.qt.digia.com/qq/qq25-formlayout.html

生成的表格如下所示:

fixed dialog

仍然需要一两个调整,但至少按钮位于正确的位置:D .

qt
1个回答
3
投票

问题是您手动创建了布局,但没有向其中添加按钮框。我建议您使用设计器创建整个表单,或者手动完成所有操作,但不要混合两者。

与设计师

  • 在 Designer 中打开 .ui 文件。
  • 右键单击设置网格布局。
  • 拖放文本框。

手册

© www.soinside.com 2019 - 2024. All rights reserved.