中文提示的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. |