QT之中文messageBox

中文提示的messageBox

QT创建messageBox可以直接调用QMessageBox中的静态成员函数,如QMessageBox::information,QMessageBox::warning等,但如果要实现中文的按钮,可以使用以下方法:

#include <QWidget>
#include <QString>

int myMessageBox(QWidget *parent, QMessageBox::Icon icon, QString title, QString text, QString informativeText)
{
    QMessageBox msgBox(parent);
    msgBox.setIcon(icon);
    msgBox.setWindowTitle(title);
    msgBox.setText(text);
    msgBox.setInformativeText(informativeText);
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setButtonText(QMessageBox::Ok, QStringLiteral("确定"));
    msgBox.setButtonText(QMessageBox::Cancel, QStringLiteral("取消"));
    return msgBox.exec();
}

其中第一个参数指定父对象,第二个参数可以取如下值:

    enum QMessageBox::Icon

This enum has the following values:


Constant

Value

Description

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.

第三、四、五个参数分别为标题、提示信息、详细信息,返回值为int类型代表按下按键的值,此函数将按键写死了,可以根据自己需求进行更改。

另外

可以根据自己需要添加按键,调用QMessageBox::addButton()函数,该函数可以添加已有按键,见枚举类型enum QMessageBox::StandButton,如msgBox.addButton(QMessageBox::Abort);

也可以添加新的按键,并指定按键角色,见枚举类型enum QMessageBox::ButtonRole,如 msgBox.addButton(tr("Connect"), QMessageBox::ActionRole);

其实,已有按键也分别对应着一个按键角色:如下表


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


Constant

Value

Description

QMessageBox::Ok

0x00000400

An "OK" button defined with the AcceptRole.

QMessageBox::Open

0x00002000

An "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.


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值