Qt4.6 QMessageBox 使用大全

使用方式为
/***消息框的使用 *********************/
//提示框
//QMessageBox::about(this,"about me","helloworld");
// QMessageBox::aboutQt(this,"about me");  //输出关于Qt的详细信息
//警告窗口
//int bs=   QMessageBox::critical(this,"about me","show info",QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok);
//ui.infos->setText(QString(tr("%1")).arg(bs));

//输出信息提示框
//int bs=  QMessageBox::information(this,"about me","information",QMessageBox::Ok|QMessageBox::Cancel|QMessageBox::Discard|QMessageBox::Save,QMessageBox::Ok);
//ui.infos->setText(QString(tr("%1")).arg(bs));

其他使用方式:
QMessageBox msgBox;
 msgBox.setText("The document has been modified.");
 msgBox.setInformativeText("Do you want to save your changes?");
 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
 msgBox.setDefaultButton(QMessageBox::Save);
 int ret = msgBox.exec();
switch (ret) {
   case QMessageBox::Save:
       // Save was clicked
       break;
   case QMessageBox::Discard:
       // Don't Save was clicked
       break;
   case QMessageBox::Cancel:
       // Cancel was clicked
       break;
   default:
       // should never be reached
       break;
 }

QuestionFor asking a question during normal operations.

InformationFor reporting information about normal operations.

WarningFor reporting non-critical errors.

CriticalFor reporting critical errors.
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);



 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
 }
ConstantValueDescription
QMessageBox::InvalidRole-1The button is invalid.
QMessageBox::AcceptRole0Clicking the button causes the dialog to be accepted (e.g. OK).
QMessageBox::RejectRole1Clicking the button causes the dialog to be rejected (e.g. Cancel).
QMessageBox::DestructiveRole2Clicking the button causes a destructive change (e.g. for Discarding Changes) and closes the dialog.
QMessageBox::ActionRole3Clicking the button causes changes to the elements within the dialog.
QMessageBox::HelpRole4The button can be clicked to request help.
QMessageBox::YesRole5The button is a "Yes"-like button.
QMessageBox::NoRole6The button is a "No"-like button.
QMessageBox::ApplyRole8The button applies current changes.
QMessageBox::ResetRole7The button resets the dialog's fields to default values.

See also StandardButton.


enum QMessageBox::Icon

This enum has the following values:

ConstantValueDescription
QMessageBox::NoIcon0the message box does not have any icon.
QMessageBox::Question4an icon indicating that the message is asking a question.
QMessageBox::Information1an icon indicating that the message is nothing out of the ordinary.
QMessageBox::Warning2an icon indicating that the message is a warning, but can be dealt with.
QMessageBox::Critical3an 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.

ConstantValueDescription
QMessageBox::Ok0x00000400An "OK" button defined with the AcceptRole.
QMessageBox::Open0x00002000A "Open" button defined with the AcceptRole.
QMessageBox::Save0x00000800A "Save" button defined with the AcceptRole.
QMessageBox::Cancel0x00400000A "Cancel" button defined with the RejectRole.
QMessageBox::Close0x00200000A "Close" button defined with the RejectRole.
QMessageBox::Discard0x00800000A "Discard" or "Don't Save" button, depending on the platform, defined with theDestructiveRole.
QMessageBox::Apply0x02000000An "Apply" button defined with the ApplyRole.
QMessageBox::Reset0x04000000A "Reset" button defined with the ResetRole.
QMessageBox::RestoreDefaults0x08000000A "Restore Defaults" button defined with the ResetRole.
QMessageBox::Help0x01000000A "Help" button defined with the HelpRole.
QMessageBox::SaveAll0x00001000A "Save All" button defined with the AcceptRole.
QMessageBox::Yes0x00004000A "Yes" button defined with the YesRole.
QMessageBox::YesToAll0x00008000A "Yes to All" button defined with the YesRole.
QMessageBox::No0x00010000A "No" button defined with the NoRole.
QMessageBox::NoToAll0x00020000A "No to All" button defined with the NoRole.
QMessageBox::Abort0x00040000An "Abort" button defined with the RejectRole.
QMessageBox::Retry0x00080000A "Retry" button defined with the AcceptRole.
QMessageBox::Ignore0x00100000An "Ignore" button defined with the AcceptRole.
QMessageBox::NoButton0x00000000An invalid button.

The following values are obsolete:

ConstantValueDescription
QMessageBox::YesAllYesToAllUse YesToAll instead.
QMessageBox::NoAllNoToAllUse NoToAll instead.
QMessageBox::Default0x00000100Use the defaultButton argument of information(), warning(), etc. instead, or call setDefaultButton().
QMessageBox::Escape0x00000200Call setEscapeButton() instead.
QMessageBox::FlagMask0x00000300
QMessageBox::ButtonMask~FlagMask
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值