QDialogButtonBox 类

QDialogButtonBox 类

QDialogButtongBox类是一个包含很多按钮的控件,在对话框中有多个按钮需要分组排列的按钮时,可以使用QDialogButtongBox类。

对话框或者消息框中的按钮布局,不同平台风格不同。开发人员可以向QDialogButtonBox添加按钮,在添加后QDialogButtonBox会为用户自动使用合适的布局。

 

Header:

#include <QDialogButtonBox>

qmake:

QT += widgets

Since:

Qt 4.2

Inherits:

QWidget.

成员列表(包含继承的成员)

公有类型(PublicTypes)

enum

ButtonLayout { WinLayout, MacLayout, KdeLayout, GnomeLayout }

enum

ButtonRole { InvalidRole, AcceptRole, RejectRole, DestructiveRole, ..., ResetRole }

enum

StandardButton { Ok, Open, Save, Cancel, ..., NoButton }

flags

StandardButtons

属性(Properties)

centerButtons : bool

orientation : Qt::Orientation

standardButtons : StandardButtons

59 properties inherited from QWidget

1 property inherited from QObject

公有函数(Public Functions)

 

QDialogButtonBox(QWidget * parent = 0)

 

QDialogButtonBox(Qt::Orientation orientation, QWidget * parent = 0)

 

QDialogButtonBox(StandardButtons buttons, QWidget * parent = 0)

 

QDialogButtonBox(StandardButtons buttons, Qt::Orientation orientation, QWidget * parent = 0)

 

~QDialogButtonBox()

void

addButton(QAbstractButton * button, ButtonRole role)

QPushButton *

addButton(const QString & text, ButtonRole role)

QPushButton *

addButton(StandardButton button)

QPushButton *

button(StandardButton which) const

ButtonRole

buttonRole(QAbstractButton * button) const

QList<QAbstractButton *>

buttons() const

bool

centerButtons() const

void

clear()

Qt::Orientation

orientation() const

void

removeButton(QAbstractButton * button)

void

setCenterButtons(bool center)

void

setOrientation(Qt::Orientation orientation)

void

setStandardButtons(StandardButtons buttons)

StandardButton

standardButton(QAbstractButton * button) const

StandardButtons

standardButtons() const

217 public functions inherited from QWidget

31 public functions inherited from QObject

13 public functions inherited from QPaintDevice

信号

void

accepted()

void

clicked(QAbstractButton * button)

void

helpRequested()

void

rejected()

4 signals inherited from QWidget

2 signals inherited from QObject

重载的保护函数(Reimplemented Protected Functions)

virtual void

changeEvent(QEvent * event)

virtual bool

event(QEvent * event)

37 protected functions inherited from QWidget

9 protected functions inherited from QObject

1 protected function inherited from QPaintDevice

其他继承来的成员(Additional Inherited Members)

19 public slots inherited from QWidget

1 public slot inherited from QObject

1 public variable inherited from QObject

5 static public members inherited from QWidget

10 static public members inherited from QObject

37 protected functions inherited from QWidget

9 protected functions inherited from QObject

1 protected function inherited from QPaintDevice

1 protected slot inherited from QWidget

2 protected variables inherited from QObject

1 protected type inherited from QPaintDevice

 

对话框按钮的角色

对话框的按钮大多有一些角色(roles),包括:

Ø 接受或拒绝;

Ø 帮助;

Ø 完成特定功能;

按钮的角色可以通过ButtonRole()返回的枚举值得到,一个按钮可以同时使用多个角色,通过多个角色的flag的组合来实现。具体如下:

 

enum QDialogButtonBox::​ButtonRole

 

Constant

Value

Description

QDialogButtonBox::InvalidRole

-1

The button is invalid.

QDialogButtonBox::AcceptRole

0

Clicking the button causes the dialog to be accepted (e.g. OK).

QDialogButtonBox::RejectRole

1

Clicking the button causes the dialog to be rejected (e.g. Cancel).

QDialogButtonBox::DestructiveRole

2

Clicking the button causes a destructive change (e.g. for Discarding Changes) and closes the dialog.

QDialogButtonBox::ActionRole

3

Clicking the button causes changes to the elements within the dialog.

QDialogButtonBox::HelpRole

4

The button can be clicked to request help.

QDialogButtonBox::YesRole

5

The button is a "Yes"-like button.

QDialogButtonBox::NoRole

6

The button is a "No"-like button.

QDialogButtonBox::ApplyRole

8

The button applies current changes.

QDialogButtonBox::ResetRole

7

The button resets the dialog's fields to default values.

 

对话框中的标准按钮

对于对话框中的标准按钮(如,OKCancelSave)可以在QDialogButtonBox创建时,使用flag标志的或运算指定。

 

enum QDialogButtonBox::​StandardButton
flags QDialogButtonBox::​StandardButtons

下面的这些枚举值用来描述标准按钮。每一个按钮有一个已经定义好的ButtonRole.

Constant

Value

Description

QDialogButtonBox::Ok

0x00000400

An "OK" button defined with the AcceptRole.

QDialogButtonBox::Open

0x00002000

An "Open" button defined with the AcceptRole.

QDialogButtonBox::Save

0x00000800

A "Save" button defined with the AcceptRole.

QDialogButtonBox::Cancel

0x00400000

A "Cancel" button defined with the RejectRole.

QDialogButtonBox::Close

0x00200000

A "Close" button defined with the RejectRole.

QDialogButtonBox::Discard

0x00800000

A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole.

QDialogButtonBox::Apply

0x02000000

An "Apply" button defined with the ApplyRole.

QDialogButtonBox::Reset

0x04000000

A "Reset" button defined with the ResetRole.

QDialogButtonBox::RestoreDefaults

0x08000000

A "Restore Defaults" button defined with the ResetRole.

QDialogButtonBox::Help

0x01000000

A "Help" button defined with the HelpRole.

QDialogButtonBox::SaveAll

0x00001000

A "Save All" button defined with the AcceptRole.

QDialogButtonBox::Yes

0x00004000

A "Yes" button defined with the YesRole.

QDialogButtonBox::YesToAll

0x00008000

A "Yes to All" button defined with the YesRole.

QDialogButtonBox::No

0x00010000

A "No" button defined with the NoRole.

QDialogButtonBox::NoToAll

0x00020000

A "No to All" button defined with the NoRole.

QDialogButtonBox::Abort

0x00040000

An "Abort" button defined with the RejectRole.

QDialogButtonBox::Retry

0x00080000

A "Retry" button defined with the AcceptRole.

QDialogButtonBox::Ignore

0x00100000

An "Ignore" button defined with the AcceptRole.

QDialogButtonBox::NoButton

0x00000000

An invalid button.

The StandardButtons type is a typedef for QFlags<StandardButton>. It stores an OR combination of StandardButton values.

例如:

对话框中的普通按钮

对于对话框中的普通按钮,按照如下方式创建:自己创建按钮(button),让后将按钮添加到button box, 并制定按钮的作用(role)

例如:

    findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);
 
    moreButton = new QPushButton(tr("&More"));
    moreButton->setCheckable(true);
    moreButton->setAutoDefault(false);

可以混合搭配使用普通按钮和标准按钮。

对话框中按钮的布局

另外,QDialogButtongBox中的按钮,可以水平排列也可以竖直排列。在创建 QDialogButtongBox时,通过指定参数Qt::Orientation orientation 来指定按钮的布局方式。

 例如:

buttonBox = new QDialogButtonBox(Qt::Horizontal);

QPushButton *mkdirButton = buttonBox->addButton(
            tr("&Create Directory..."), QDialogButtonBox::ActionRole);
QPushButton *removeButton = buttonBox->addButton(tr("&Remove"),
            QDialogButtonBox::ActionRole);
buttonBox->addButton(tr("&Quit"), QDialogButtonBox::AcceptRole);

connect(mkdirButton, SIGNAL(clicked()),
            this, SLOT(createDirectory()));
connect(removeButton, SIGNAL(clicked()), this, SLOT(remove()));
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));



对话框中,三个按钮的显示效果如下:


 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在使用QDialogButtonBox时,需要为其信号accepted和rejected分别连接槽函数accept()和reject(),用以响应用户点击“确定”或“取消”按钮的操作。这两个槽函数在QWidget中定义,因此可以在自定义对话框的中直接重写这两个槽函数。 例如,如果你的自定义对话框名为MyDialog,可以在其头文件中添加如下代码: ```cpp class MyDialog : public QDialog { Q_OBJECT public: explicit MyDialog(QWidget *parent = nullptr); private slots: void accept() override; void reject() override; private: QDialogButtonBox *buttonBox; }; ``` 上述代码中,重写了accept()和reject()槽函数,并将其声明为private slots。在对应的源文件中,可以实现这两个槽函数: ```cpp MyDialog::MyDialog(QWidget *parent) : QDialog(parent) { // 创建按钮盒子,并添加“确定”和“取消”按钮 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, &QDialogButtonBox::accepted, this, &MyDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &MyDialog::reject); // 将按钮盒子添加到对话框中 QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addWidget(buttonBox); } void MyDialog::accept() { // 处理用户点击“确定”按钮的操作 QDialog::accept(); // 调用父的accept()槽函数,关闭对话框 } void MyDialog::reject() { // 处理用户点击“取消”按钮的操作 QDialog::reject(); // 调用父的reject()槽函数,关闭对话框 } ``` 在上述代码中,accept()和reject()槽函数分别处理用户点击“确定”和“取消”按钮的操作,同时调用父的accept()和reject()槽函数,以关闭对话框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值