Qt:Документация 4.3.2/qmessagebox

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск
40px Внимание: Актуальная версия перевода документации находится здесь

__NOTOC__

Image:qt-logo.png

Главная · Все классы · Основные классы · Классы по группам · Модули · Функции

Image:trolltech-logo.png

Содержание

[править] QMessageBox Class Reference
[модуль QtGui ]

The QMessageBox class provides a modal dialog with a short message, an icon, and buttons laid out depending on the current style. Далее...

 #include <QMessageBox>

Наследует QDialog.

[править] Открытые типы

  • enum ButtonRole { InvalidRole, AcceptRole, RejectRole, DestructiveRole, ..., ResetRole }
  • enum Icon { NoIcon, Question, Information, Warning, Critical }
  • enum StandardButton { Ok, Open, Save, Cancel, ..., ButtonMask }
  • flags StandardButtons

[править] Свойства

  • 2 войства, унаследованные от QDialog
  • 56 свойств, унаследованных от QWidget
  • 1 свойство, унаследованное от QObject

[править] Открытые функции

  • 5 открытых функций, унаследованных от QDialog
  • 201 свойство, унаследованное от QWidget
  • 29 открытых функций, унаследованных от QObject
  • 12 открытых функций, унаследованных от QPaintDevice

[править] Открытые слоты

  • 4 открытых слота, унаследованные от QDialog
  • 19 открытых слотов, унаследованных от QWidget
  • 1 открытый слот, унаследованный от QObject

[править] Статические открытые члены

  • void about ( QWidget * parent, const QString & title, const QString & text )
  • void aboutQt ( QWidget * parent, const QString & title = QString() )
  • StandardButton critical ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
  • StandardButton information ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
  • StandardButton question ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
  • StandardButton warning ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
  • 4 статических открытых члена, унаследованных от QWidget
  • 5 статических открытых членов, унаследованных от QObject

[править] Macros

[править] Дополнительные унаследованные члены

  • 3 сигнала, унаследованные от QDialog
  • 1 сигнал, унаследованный от QWidget
  • 1 сигнал, унаследованный от QObject
  • 38 защищенных функций, унаследованных от QWidget
  • 7 защищенных функций, унаследованных от QObject
  • 1 защищенная функция, унаследованных от QPaintDevice
  • 1 защищенный слот, унаследованый от QWidget

[править] Подробное описание

The QMessageBox class provides a modal dialog with a short message, an icon, and buttons laid out depending on the current style.

Message boxes are used to provide informative messages and to ask simple questions.

[править] Basic Usage

The easiest way to pop up a message box in Qt is to call one of the static functions QMessageBox::information(), QMessageBox::question(), QMessageBox::critical(), and QMessageBox::warning(). Пример:

 int ret = QMessageBox::warning(this, tr("My Application"),
                   tr("The document has been modified.\n"
                      "Do you want to save your changes?"),
                   QMessageBox::Save | QMessageBox::Discard
                   | QMessageBox::Cancel,
                   QMessageBox::Save);

Buttons are specified by combining StandardButtons using the bitwise OR operator. The order of the buttons on screen is platform-dependent. For example, on Windows, Save is displayed to the left of Cancel, whereas on Mac OS, the order is reversed.

The text part of all message box messages can be either rich text or plain text. With certain strings that contain XML meta characters, the auto-rich text detection may fail, interpreting plain text incorrectly as rich text. In these rare cases, use Qt::convertFromPlainText() to convert your plain text string to a visually equivalent rich text string or set the text format explicitly with setTextFormat().

Note that the Microsoft Windows User Interface Guidelines recommend using the application name as the window's title.

The Standard Dialogs example shows how to use QMessageBox as well as other built-in Qt dialogs.

[править] Severity Levels

QMessageBox supports four severity levels, indicated by an icon:


Файл:Qmessagebox-quest.png

Question For message boxes that ask a question as part of normal operation. Some style guides recommend using Information for this purpose.

Файл:Qmessagebox-info.png

Information For message boxes that are part of normal operation.

Файл:Qmessagebox-warn.png

Warning For message boxes that tell the user about unusual errors.

Файл:Qmessagebox-crit.png

Critical For message boxes that tell the user about critical errors.

[править] Advanced Usage

If the convenience static functions, such as QMessageBox::information() and QMessageBox::warning(), are not flexible enough for your needs, you can instantiate a QMessageBox on the stack. You can then use addButton() to add buttons with standard or arbitrary text.

When using an instance of QMessageBox with standard buttons, you can test the return value of exec() to determine which button was clicked. For example,

 QMessageBox msgBox;
 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
 switch (msgBox.exec()) {
 case QMessageBox::Yes:
     // yes was clicked
     break;
 case QMessageBox::No:
     // no was clicked
     break;
 default:
     // should never be reached
     break;
 }

When using an instance of QMessageBox with custom buttons, you can test the value of clickedButton() after calling exec(). For example,

 QMessageBox msgBox;
 QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole);
 QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort);
 
 msgBox.exec();
 
 if (msgBox.clickedButton() == connectButton) {
     // connect
 } else if (msgBox.clickedButton() == abortButton) {
     // abort
 }

In the example above, the Connect button is created using the addButton() overload that takes a text and a ButtonRole. The ButtonRole is used by QMessageBox to determine the ordering of the buttons on screen (which varies according to the platform).

The text(), icon() and iconPixmap() functions provide access to the current text and pixmap of the message box. The setText(), setIcon() and setIconPixmap() let you change it. The difference between setIcon() and setIconPixmap() is that the former accepts a QMessageBox::Icon and can be used to set standard icons, whereas the latter accepts a QPixmap and can be used to set custom icons.

setButtonText() and buttonText() provide access to the buttons.

[править] Default and Escape Keys

The default button (i.e., the button that is activated when the user presses Enter) can be specified using setDefaultButton(). If none is specified, QMessageBox will try to find one automatically based on the ButtonRoles of the buttons in the dialog.

Similarly, the escape button (the button that is activated when the user presses Esc) is specified using setEscapeButton(). If no escape button is specified, QMessageBox attempts to automatically detect an escape button as follows:

  1. If there is only one button, it is made the escape button.
  2. If there is a Cancel button, it is made the escape button.
  3. On Mac OS X only, if there is exactly one button with the role QMessageBox::RejectRole, it is made the escape button.

When an escape button could not be automatically detected, pressing Esc has no effect.

See also QDialogButtonBox, GUI Design Handbook: Message Box, Standard Dialogs Example, and Application Example.


[править] Описание типов

[править]
enum QMessageBox::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.


Константа Значение Описание
QMessageBox::InvalidRole -1 The button is invalid.
QMessageBox::AcceptRole 0 Clicking the button causes the dialog to be accepted (e.g. OK).
QMessageBox::RejectRole 1 Clicking the button causes the dialog to be rejected (e.g. Cancel).
QMessageBox::DestructiveRole 2 Clicking the button causes a destructive change (e.g. for Discarding Changes) and closes the dialog.
QMessageBox::ActionRole 3 Clicking the button causes changes to the elements within the dialog, without closing the dialog.
QMessageBox::HelpRole 4 The button can be clicked to request help.
QMessageBox::YesRole 5 The button is a "Yes"-like button.
QMessageBox::NoRole 6 The button is a "No"-like button.
QMessageBox::ApplyRole 8 The button applies current changes.
QMessageBox::ResetRole 7 The button resets the dialog's fields to default values.

See also StandardButton.

[править]
enum QMessageBox::Icon

This enum has the following values:


Константа Значение Описание
QMessageBox::NoIcon 0 the message box does not have any icon.
QMessageBox::Question 4 an icon indicating that the message is asking a question.
QMessageBox::Information 1 an icon indicating that the message is nothing out of the ordinary.
QMessageBox::Warning 2 an icon indicating that the message is a warning, but can be dealt with.
QMessageBox::Critical 3 an icon indicating that the message represents a critical problem.

[править]
enum QMessageBox::StandardButton
flags QMessageBox::StandardButtons

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


Константа Значение Описание
QMessageBox::Ok 0x00000400 An "OK" button defined with the AcceptRole.
QMessageBox::Open 0x00002000 A "Open" button defined with the AcceptRole.
QMessageBox::Save 0x00000800 A "Save" button defined with the AcceptRole.
QMessageBox::Cancel 0x00400000 A "Cancel" button defined with the RejectRole.
QMessageBox::Close 0x00200000 A "Close" button defined with the RejectRole.
QMessageBox::Discard 0x00800000 A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole.
QMessageBox::Apply 0x02000000 An "Apply" button defined with the ApplyRole.
QMessageBox::Reset 0x04000000 A "Reset" button defined with the ResetRole.
QMessageBox::RestoreDefaults 0x08000000 A "Restore Defaults" button defined with the ResetRole.
QMessageBox::Help 0x01000000 A "Help" button defined with the HelpRole.
QMessageBox::SaveAll 0x00001000 A "Save All" button defined with the AcceptRole.
QMessageBox::Yes 0x00004000 A "Yes" button defined with the YesRole.
QMessageBox::YesToAll 0x00008000 A "Yes to All" button defined with the YesRole.
QMessageBox::No 0x00010000 A "No" button defined with the NoRole.
QMessageBox::NoToAll 0x00020000 A "No to All" button defined with the NoRole.
QMessageBox::Abort 0x00040000 An "Abort" button defined with the RejectRole.
QMessageBox::Retry 0x00080000 A "Retry" button defined with the AcceptRole.
QMessageBox::Ignore 0x00100000 An "Ignore" button defined with the AcceptRole.
QMessageBox::NoButton 0x00000000 An invalid button.

The following values are obsolete:


Константа Значение Описание
QMessageBox::YesAll YesToAll Use YesToAll instead.
QMessageBox::NoAll NoToAll Use NoToAll instead.
QMessageBox::Default 0x00000100 Use the defaultButton argument of information(), warning(), etc. instead, or call setDefaultButton().
QMessageBox::Escape 0x00000200 Call setEscapeButton() instead.
QMessageBox::FlagMask 0x00000300
QMessageBox::ButtonMask ~FlagMask

This enum was introduced in Qt 4.2.

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

See also ButtonRole and standardButtons.


[править] Описание cвойств

[править]
detailedText : QString

This property holds the text to be displayed in the details area.

The text will be interpreted as a plain text. The default value of this property is an empty string.

Это свойство было введено в Qt 4.2.

Функции доступа:

  • QString detailedText () const
  • void setDetailedText ( const QString & text )

[править]
icon : Icon

This property holds the message box's icon.

The icon of the message box can be one of the following predefined icons:

The actual pixmap used for displaying the icon depends on the current GUI style. You can also set a custom pixmap icon using the QMessageBox::iconPixmap property. The default icon is QMessageBox::NoIcon.

Функции доступа:

  • Icon icon () const
  • void setIcon ( Icon )

See also iconPixmap.

[править]
iconPixmap : QPixmap

This property holds the current icon.

The icon currently used by the message box. Note that it's often hard to draw one pixmap that looks appropriate in all GUI styles; you may want to supply a different pixmap for each platform.

Функции доступа:

  • QPixmap iconPixmap () const
  • void setIconPixmap ( const QPixmap & pixmap )

See also icon.

[править]
informativeText : QString

This property holds the informative text that provides a fuller description for the message.

Infromative text can be used to expand upon the text() to give more information to the user. On the Mac, this text appears in small system font below the text(). On other platforms, it is simply appended to the existing text.

Это свойство было введено в Qt 4.2.

Функции доступа:

  • QString informativeText () const
  • void setInformativeText ( const QString & text )

[править]
standardButtons : StandardButtons

This property holds collection of standard buttons in the message box.

This property controls which standard buttons are used by the message box.

Это свойство было введено в Qt 4.2.

Функции доступа:

  • StandardButtons standardButtons () const
  • void setStandardButtons ( StandardButtons buttons )

See also addButton().

[править]
text : QString

This property holds the message box text to be displayed.

The text will be interpreted either as a plain text or as rich text, depending on the text format setting ( QMessageBox::textFormat). The default setting is Qt::AutoText, i.e. the message box will try to auto-detect the format of the text.

The default value of this property is an empty string.

Функции доступа:

  • QString text () const
  • void setText ( const QString & text )

See also textFormat.

[править]
textFormat : Qt::TextFormat

This property holds the format of the text displayed by the message box.

The current text format used by the message box. See the Qt::TextFormat enum for an explanation of the possible options.

The default format is Qt::AutoText.

Функции доступа:

  • Qt::TextFormat textFormat () const
  • void setTextFormat ( Qt::TextFormat format )

See also setText().


[править] Описание функций-членов

[править]
QMessageBox::QMessageBox ( QWidget * parent = 0 )

Constructs a message box with no text and no buttons.

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

The parent argument is passed to the QDialog constructor.

[править]
QMessageBox::QMessageBox ( Icon icon, const QString & title, const QString & text, StandardButtons buttons = NoButton, QWidget * parent = 0, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint )

Constructs a message box with the given icon, title, text, and standard buttons. (Buttons can also be added at any time using addButton().)

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

The parent and f arguments are passed to the QDialog constructor.

See also setWindowTitle(), setText(), setIcon(), and setStandardButtons().

[править]
QMessageBox::~QMessageBox ()

Destroys the message box.

[править]
void QMessageBox::about ( QWidget * parent, const QString & title, const QString & text ) [static]

Displays a simple about box with title title and text text. The about box's parent is parent.

about() looks for a suitable icon in four locations:

  1. It prefers parent->icon() if that exists.
  2. If not, it tries the top-level widget containing parent.
  3. If that fails, it tries the active window.
  4. As a last resort it uses the Information icon.

The about box has a single button labelled "OK".

See also QWidget::windowIcon() and QApplication::activeWindow().

[править]
void QMessageBox::aboutQt ( QWidget * parent, const QString & title = QString() ) [static]

Displays a simple message box about Qt, with the given title and centered over parent (if parent is not 0). The message includes the version number of Qt being used by the application.

This is useful for inclusion in the Help menu of an application, as shown in the Menus example.

QApplication provides this functionality as a slot.

See also QApplication::aboutQt().

[править]
void QMessageBox::addButton ( QAbstractButton * button, ButtonRole role )

Adds the given button to the message box with the specified role.

Эта функция была введена в Qt 4.2.

See also removeButton(), button(), and setStandardButtons().

[править]
QPushButton * QMessageBox::addButton ( const QString & text, ButtonRole role )

Эта перегруженная функция предоставлена для удобства.

Creates a button with the given text, adds it to the message box for the specified role, and returns it.

Эта функция была введена в Qt 4.2.

[править]
QPushButton * QMessageBox::addButton ( StandardButton button )

Эта перегруженная функция предоставлена для удобства.

Adds a standard button to the message box if it is valid to do so, and returns the push button.

Эта функция была введена в Qt 4.2.

See also setStandardButtons().

[править]
QAbstractButton * QMessageBox::button ( StandardButton which ) const

Returns a pointer corresponding to the standard button which, or 0 if the standard button doesn't exist in this message box.

Эта функция была введена в Qt 4.2.

See also standardButtons and standardButton().

[править]
QAbstractButton * QMessageBox::clickedButton () const

Returns the button that was clicked by the user, or 0 if the user hit the Esc key and no escape button was set.

If exec() hasn't been called yet, returns 0.

Пример:

 QMessageBox messageBox(this);
 QAbstractButton *disconnectButton =
       messageBox.addButton(tr("Disconnect"), QMessageBox::ActionRole);
 ...
 messageBox.exec();
 if (messageBox.clickedButton() == disconnectButton) {
     ...
 }

Эта функция была введена в Qt 4.2.

See also standardButton() and button().

[править]
StandardButton QMessageBox::critical ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton ) [static]

Opens a critical message box with the title title and the text text. The standard buttons buttons is added to the message box. defaultButton specifies the button be used as the defaultButton. If the defaultButton is set to QMessageBox::NoButton, QMessageBox picks a suitable default automatically.

Returns the identity of the standard button that was activated. If Esc was pressed, returns the escape button (if any).

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

Эта функция была введена в Qt 4.2.

See also question(), warning(), and information().

[править]
QPushButton * QMessageBox::defaultButton () const

Returns the button that should be the message box's default button. Returns 0 if no default button was set.

Эта функция была введена в Qt 4.2.

See also setDefaultButton(), addButton(), and QPushButton::setDefault().

[править]
QAbstractButton * QMessageBox::escapeButton () const

Returns the button that is activated when escape is pressed.

By default, QMessageBox attempts to automatically detect an escape button as follows:

  1. If there is only one button, it is made the escape button.
  2. If there is a Cancel button, it is made the escape button.
  3. On Mac OS X only, if there is exactly one button with the role QMessageBox::RejectRole, it is made the escape button.

When an escape button could not be automatically detected, pressing Esc has no effect.

Эта функция была введена в Qt 4.2.

See also setEscapeButton() and addButton().

[править]
int QMessageBox::exec () [slot]

Shows the message box as a modal dialog, blocking until the user closes it.

When using a QMessageBox with standard buttons, this functions returns a StandardButton value indicating the standard button that was clicked. When using QMessageBox with custom buttons, this function returns an opaque value; use clickedButton() to determine which button was clicked.

Users cannot interact with any other window in the same application until they close the dialog, either by clicking a button or by using a mechanism provided by the window system.

See also show() and result().

[править]
StandardButton QMessageBox::information ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton ) [static]

Opens an information message box with the title title and the text text. The standard buttons buttons is added to the message box. defaultButton specifies the button be used as the defaultButton. If the defaultButton is set to QMessageBox::NoButton, QMessageBox picks a suitable default automatically.

Returns the identity of the standard button that was activated. If Esc was pressed, returns the escape button (if any).

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

Эта функция была введена в Qt 4.2.

See also question(), warning(), and critical().

[править]
StandardButton QMessageBox::question ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton ) [static]

Opens a question message box with the title title and the text text. The standard buttons buttons is added to the message box. defaultButton specifies the button be used as the defaultButton. If the defaultButton is set to QMessageBox::NoButton, QMessageBox picks a suitable default automatically.

Returns the identity of the standard button that was activated. If Esc was pressed, returns the escape button (if any).

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

Эта функция была введена в Qt 4.2.

See also information(), warning(), and critical().

[править]
void QMessageBox::removeButton ( QAbstractButton * button )

Removes button from the button box without deleting it.

Эта функция была введена в Qt 4.2.

See also addButton() and setStandardButtons().

[править]
void QMessageBox::setDefaultButton ( QPushButton * button )

Sets the message box's default button to button.

Эта функция была введена в Qt 4.2.

See also defaultButton(), addButton(), and QPushButton::setDefault().

[править]
void QMessageBox::setDefaultButton ( StandardButton button )

Эта перегруженная функция предоставлена для удобства.

Sets the message box's default button to button.

Эта функция была введена в Qt 4.3.

See also addButton() and QPushButton::setDefault().

[править]
void QMessageBox::setEscapeButton ( QAbstractButton * button )

Sets the button that gets activated when the Escape key is pressed to button.

Эта функция была введена в Qt 4.2.

See also escapeButton(), addButton(), and clickedButton().

[править]
void QMessageBox::setEscapeButton ( StandardButton button )

Эта перегруженная функция предоставлена для удобства.

Sets the buttons that gets activated when the Escape key is pressed to button.

Эта функция была введена в Qt 4.3.

See also addButton() and clickedButton().

[править]
void QMessageBox::setWindowModality ( Qt::WindowModality windowModality )

This function shadows QWidget::setWindowModality().

Sets the modality of the message box to windowModality.

On Mac OS X, if the modality is set to Qt::WindowModal and the message box has a parent, then the message box will be a Qt::Sheet, otherwise the message box will be a standard dialog.

Эта функция была введена в Qt 4.2.

[править]
void QMessageBox::setWindowTitle ( const QString & title )

This function shadows QWidget::setWindowTitle().

Sets the title of the message box to title. On Mac OS X, the window title is ignored (as required by the Mac OS X Guidelines).

Эта функция была введена в Qt 4.2.

[править]
StandardButton QMessageBox::standardButton ( QAbstractButton * button ) const

Returns the standard button enum value corresponding to the given button, or NoButton if the given button isn't a standard button.

Эта функция была введена в Qt 4.2.

See also button() and standardButtons().

[править]
StandardButton QMessageBox::warning ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton ) [static]

Opens a warning message box with the title title and the text text. The standard buttons buttons is added to the message box. defaultButton specifies the button be used as the defaultButton. If the defaultButton is set to QMessageBox::NoButton, QMessageBox picks a suitable default automatically.

Returns the identity of the standard button that was activated. If Esc was pressed, returns the escape button (if any).

If parent is 0, the message box becomes an application-global modal dialog box. If parent is a widget, the message box becomes modal relative to parent.

Эта функция была введена в Qt 4.2.

See also question(), information(), and critical().


[править] Macro Documentation

[править]
QT_REQUIRE_VERSION ( int argc, char ** argv, const char * version )

This macro can be used to ensure that the application is run against a recent enough version of Qt. This is especially useful if your application depends on a specific bug fix introduced in a bug-fix release (e.g., 4.0.2).

The argc and argv parameters are the main() function's argc and argv parameters. The version parameter is a string literal that specifies which version of Qt the application requires (e.g., "4.0.2").

Пример:

 #include <QApplication>
 #include <QMessageBox>
 
 int main(int argc, char *argv[])
 {
     QT_REQUIRE_VERSION(argc, argv, "4.0.2")
 
     QApplication app(argc, argv);
     ...
     return app.exec();
 }

Copyright © 2007 Trolltech Trademarks
Qt 4.3.2