QDialogButtonBox Class Reference

[ QtGui module]

The QDialogButtonBox class is a widget that presents buttons in a layout that is appropriate to the current widget style. 更多...

继承 QWidget .

类型

方法

Qt Signals


详细描述

The QDialogButtonBox class is a widget that presents buttons in a layout that is appropriate to the current widget style.

Dialogs and message boxes typically present buttons in a layout that conforms to the interface guidelines for that platform. Invariably, different platforms have different layouts for their dialogs. QDialogButtonBox allows a developer to add buttons to it and will automatically use the appropriate layout for the user's desktop environment.

Most buttons for a dialog follow certain roles. Such roles include:

There can also be alternate ways of dismissing the dialog which may cause destructive results.

Most dialogs have buttons that can almost be considered standard (e.g. OK and Cancel buttons). It is sometimes convenient to create these buttons in a standard way.

There are a couple ways of using QDialogButtonBox. One ways is to create the buttons (or button texts) yourself and add them to the button box, specifying their role.

     findButton = new QPushButton(tr("&Find"));
     findButton->setDefault(true);
     moreButton = new QPushButton(tr("&More"));
     moreButton->setCheckable(true);
     moreButton->setAutoDefault(false);
     buttonBox = new QDialogButtonBox(Qt.Vertical);
     buttonBox->addButton(findButton, QDialogButtonBox.ActionRole);
     buttonBox->addButton(moreButton, QDialogButtonBox.ActionRole);
			

Alternatively, QDialogButtonBox provides several standard buttons (e.g. OK, Cancel, Save) that you can use. They exist as flags so you can OR them together in the constructor.

     buttonBox = new QDialogButtonBox(QDialogButtonBox.Ok
                                      | QDialogButtonBox.Cancel);
     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
			

You can mix and match normal buttons and standard buttons.

Currently the buttons are laid out in the following way if the button box is horizontal:

GnomeLayout Horizontal Button box laid out in horizontal GnomeLayout
KdeLayout Horizontal Button box laid out in horizontal KdeLayout
MacLayout Horizontal Button box laid out in horizontal MacLayout
WinLayout Horizontal Button box laid out in horizontal WinLayout

The buttons are laid out the following way if the button box is vertical:

GnomeLayout KdeLayout MacLayout WinLayout
GnomeLayout Vertical KdeLayout Vertical MacLayout Vertical WinLayout Vertical

Additionally, button boxes that contain only buttons with ActionRole or HelpRole 可以是 considered modeless and have an alternate look on Mac OS X:

非模态水平 MacLayout Screenshot of modeless horizontal MacLayout

When a button is clicked in the button box, the clicked () signal is emitted for the actual button is that is pressed. For convenience, if the button has an AcceptRole , RejectRole ,或 HelpRole accepted (), rejected (),或 helpRequested () signals are emitted respectively.

If you want a specific button to be default you need to call QPushButton.setDefault () on it yourself. However, if there is no default button set and to preserve which button is the default button across platforms when 使用 QPushButton.autoDefault property, the first push button with the accept role is made the default button when the QDialogButtonBox is shown,


类型文档编制

QDialogButtonBox.ButtonLayout

This enum describes the layout policy to be used when arranging the buttons contained in the button box.

常量 描述
QDialogButtonBox.WinLayout 0 Use a policy appropriate for applications on Windows.
QDialogButtonBox.MacLayout 1 Use a policy appropriate for applications on Mac OS X.
QDialogButtonBox.KdeLayout 2 Use a policy appropriate for applications on KDE.
QDialogButtonBox.GnomeLayout 3 Use a policy appropriate for applications on GNOME.

The button layout is specified by the current style . However, on the X11 platform, it may be influenced by the desktop environment.

QDialogButtonBox.ButtonRole

This enum describes the roles that can be used to describe buttons in the button box. Combinations of these roles are as flags used to describe different aspects of their behavior.

常量 描述
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.

另请参阅 StandardButton .

QDialogButtonBox.StandardButton

These enums describe flags for standard buttons. Each button has a defined ButtonRole .

常量 描述
QDialogButtonBox.Ok 0x00000400 An "OK" button defined with the AcceptRole .
QDialogButtonBox.Open 0x00002000 A "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.

另请参阅 ButtonRole and standardButtons .


方法文档编制

QDialogButtonBox.__init__ ( self , QWidget   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs an empty, horizontal button box with the given parent .

另请参阅 orientation and addButton ().

QDialogButtonBox.__init__ ( self , Qt.Orientation   orientation , QWidget   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs an empty button box with the given orientation and parent .

另请参阅 orientation and addButton ().

QDialogButtonBox.__init__ ( self , StandardButtons   buttons , Qt.Orientation   orientation  = Qt.Horizontal, QWidget   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a button box with the given orientation and parent , containing the standard buttons specified by buttons .

另请参阅 orientation and addButton ().

QDialogButtonBox.addButton ( self , QAbstractButton   button , ButtonRole   role )

button argument has it's ownership transferred to Qt.

添加给定 button to the button box with the specified role . If the role is invalid, the button is not added.

If the button has already been added, it is removed and added again with the new role.

注意: The button box takes ownership of the button.

另请参阅 removeButton () 和 clear ().

QPushButton QDialogButtonBox.addButton ( self , QString  text , ButtonRole   role )

Creates a push button with the given text , adds it to the button box for the specified role , and returns the corresponding push button. If role is invalid, no button is created, and zero is returned.

另请参阅 removeButton () 和 clear ().

QPushButton QDialogButtonBox.addButton ( self , StandardButton   button )

添加标准 button to the button box if it is valid to do so, and returns a push button. If button is invalid, it is not added to the button box, and zero is returned.

另请参阅 removeButton () 和 clear ().

QPushButton QDialogButtonBox.button ( self , StandardButton   which )

返回 QPushButton corresponding to the standard button which , or 0 if the standard button doesn't exist in this button box.

另请参阅 standardButton (), standardButtons (), and buttons ().

ButtonRole QDialogButtonBox.buttonRole ( self , QAbstractButton   button )

Returns the button role for the specified button . This function returns InvalidRole if button is 0 or has not been added to the button box.

另请参阅 buttons () 和 addButton ().

list-of-QAbstractButton QDialogButtonBox.buttons ( self )

Returns a list of all the buttons that have been added to the button box.

另请参阅 buttonRole (), addButton (),和 removeButton ().

bool QDialogButtonBox.centerButtons ( self )

QDialogButtonBox.changeEvent ( self , QEvent   event )

重实现自 QWidget.changeEvent ().

QDialogButtonBox.clear ( self )

Clears the button box, deleting all buttons within it.

另请参阅 removeButton () 和 addButton ().

bool QDialogButtonBox.event ( self , QEvent   event )

重实现自 QObject.event ().

Qt.Orientation QDialogButtonBox.orientation ( self )

QDialogButtonBox.removeButton ( self , QAbstractButton   button )

button argument

移除 button from the button box without deleting it and sets its parent to zero.

另请参阅 clear (), buttons (),和 addButton ().

QDialogButtonBox.setCenterButtons ( self , bool  center )

QDialogButtonBox.setOrientation ( self , Qt.Orientation   orientation )

QDialogButtonBox.setStandardButtons ( self , StandardButtons   buttons )

StandardButton QDialogButtonBox.standardButton ( self , QAbstractButton   button )

Returns the standard button enum value corresponding to the given button ,或 NoButton 若 given button 不是标准按钮。

另请参阅 button (), buttons (),和 standardButtons ().

StandardButtons QDialogButtonBox.standardButtons ( self )


Qt Signal Documentation

void accepted ()

This is the default overload of this signal.

This signal is emitted when a button inside the button box is clicked, as long as it was defined with the AcceptRole or YesRole .

另请参阅 rejected (), clicked (),和 helpRequested ().

void clicked (QAbstractButton*)

This is the default overload of this signal.

This signal is emitted when a button inside the button box is clicked. The specific button that was pressed is specified by button .

另请参阅 accepted (), rejected (),和 helpRequested ().

void helpRequested ()

This is the default overload of this signal.

This signal is emitted when a button inside the button box is clicked, as long as it was defined with the HelpRole .

另请参阅 accepted (), rejected (),和 clicked ().

void rejected ()

This is the default overload of this signal.

This signal is emitted when a button inside the button box is clicked, as long as it was defined with the RejectRole or NoRole .

另请参阅 accepted (), helpRequested (),和 clicked ().