赞
踩
对话框和消息框通常以符合该平台界面准则的布局呈现按钮。不同的平台总是有不同的对话框布局。 QDialogButtonBox 允许开发人员向其中添加按钮,并且会自动为用户的桌面环境使用适当的布局。
对话框的大多数按钮都遵循某些角色。这些角色包括:
大多数对话框都有几乎可以被认为是标准的按钮(例如确定和取消按钮)。有时以标准方式创建这些按钮很方便。
有几种使用 QDialogButtonBox 的方法。一种方法是自己创建按钮(或按钮文本)并将它们添加到按钮框中,指定它们的角色。
- findButton = new QPushButton(tr("&Find"));
- findButton->setDefault(true);
-
- moreButton = new QPushButton(tr("&More"));
- moreButton->setCheckable(true);
- moreButton->setAutoDefault(false);
或者,QDialogButtonBox 提供了几个可以使用的标准按钮(例如 OK、Cancel、Save)。 它们作为标志存在,因此可以在构造函数中将它们组合在一起。
- buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
-
- connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
- connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
当在按钮框中单击按钮时,会发出 clicked() 信号,用于实际按下的按钮。 为方便起见,如果按钮具有 AcceptRole、RejectRole 或 HelpRole,则分别发出 accepted()、rejected() 或 helpRequested() 信号。
如果希望特定按钮成为默认按钮,则需要自己调用 QPushButton::setDefault() 。但是,如果没有设置默认按钮并在使用 QPushButton::autoDefault 属性时保留哪个按钮是跨平台的默认按钮,则当 QDialogButtonBox 显示时,具有 AcceptRole 角色的第一个按钮将成为默认按钮,
1、enum QDialogButtonBox::ButtonLayout:此枚举描述了在排列按钮框中包含的按钮时要使用的布局策略。按钮布局由当前样式(QWidget::style())指定。
2、enum QDialogButtonBox::ButtonRole:此枚举描述了可用于描述按钮框中的按钮的角色。这些角色的组合作为用于描述其行为的不同方面的标志。
3、enum QDialogButtonBox::StandardButton:此枚举描述标准按钮的标志。每个按钮都有一个已定义的 ButtonRole。
1、centerButtons : bool
按钮框中的按钮是否居中。默认为 false。
2、orientation : Qt::Orientation
按钮框的方向。默认为水平的(即按钮并排排列)。
3、standardButtons : StandardButtons
按钮框使用哪些标准按钮。
1、【信号】void accepted()
当单击使用 AcceptRole 或 YesRole 定义的按钮时发出此信号。
2、【信号】void clicked(QAbstractButton *button)
当单击按钮框内的按钮时发出此信号。
3、【信号】void helpRequested()
当单击使用 HelpRole 定义的按钮时发出此信号。
4、【信号】void rejected()
当单击使用 RejectRole 或 NoRole 定义的按钮时发出此信号。
5、void addButton(QAbstractButton *button, QDialogButtonBox::ButtonRole role)
将给定按钮添加到具有指定角色的按钮框中。如果角色无效,则不添加按钮。
如果已添加按钮,则将其删除并使用新角色再次添加。
按钮框拥有按钮的所有权。
QPushButton * addButton(const QString &text, QDialogButtonBox::ButtonRole role)
使用给定文本创建一个按钮,将其添加到指定角色的按钮框,并返回相应的按钮。如果角色无效,则不创建按钮,并返回 nullptr。
QPushButton * addButton(QDialogButtonBox::StandardButton button)
如果 button 有效,则将标准按钮添加到按钮框,并返回一个按钮。如果 button 无效,则不添加到按钮框中,返回nullptr。
6、QPushButton * button(QDialogButtonBox::StandardButton which)
返回对应于标准按钮的 QPushButton ,如果标准按钮在此按钮框中不存在,则返回 nullptr。
7、QDialogButtonBox::ButtonRole buttonRole(QAbstractButton *button)
返回指定按钮的按钮角色。如果按钮为 nullptr 或尚未添加到按钮框,则返回 InvalidRole。
8、QList<QAbstractButton *> buttons()
返回已添加到按钮框中的所有按钮的列表。
9、void clear()
清除按钮框,删除其中的所有按钮。
10、void removeButton(QAbstractButton *button)
从按钮框中移除按钮而不删除它并将其父级设置为nullptr。
11、QDialogButtonBox::StandardButton standardButton(QAbstractButton *button)
返回与给定按钮对应的标准按钮枚举值,如果给定按钮不是标准按钮,则返回 NoButton。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。