The Qmessagebox class provides a short message, a modal dialog box with icons and buttons, a top-level layout (Qgridlayout) that includes a message icon, a message text, and a button collection
============================================================================================
Icon variable Qmessagebox::noicon no icon
Qmessagebox::information message icon
Qmessagebox::warning warning message
Qmessagebox::critical Serious
Button variable
Qmessagebox::nobutton no icon
Qmessagebox::ok OK
Qmessagebox::cancel Cancel
Qmessagebox::yes is
Qmessagebox::no No
Qmessagebox::abort Interrupt
Qmessagebox::retry Retry
Qmessagebox::ignore Cancel
Note:
Qmessagebox::D efault (performed or operated) and is hit when enter is pressed
Qmessagebox::escape (performed or operated), when ESC is pressed, the constructor is clicked
Qmessagebox::qmessagebox (qwidget *parent=0,const char *name=0)
Constructs a parent object that is parented with the name identified as name
Qmessagebox::qmessagebox (const QString &caption,const QString &text,icon Icon,
int Button0,int button1,int Button2,qwidget *parent=0,
const char *name=0,bool modal=true,wflags f=wstyle_dialogborder)
Construct a message box titled caption with text, icon and up to three buttons
Qmessagebox::~qmessagebox ()
Destroying message boxes
void Qmessagebox::about (Qwidget *parent,const QString &caption,const QString &text) Static display of a simple about box titled caption text
void Qmessagebox::aboutqt (Qwidget *parent,const QString &caption=qstring::null) static
Displays the QT version number that the application is using
int qmessagebox::critical (qwidget *parent,const QString &caption,const QString &text,int button0,int button1, int button2=0) Statically opens a critical message box titled caption with text, which has a maximum of three buttons, and if you do not want three buttons, you can set the last or the last two to Qmessagebox::nobutton
int qmessagebox::critical (qwidget *parent,const QString &caption,const QString &text,const QString & Button0text=qstring::null,const QString &button1text=qstring::null,const QString &button2text=qstring:: Null,int defaultbuttonnumber=0,int escapebuttonnumber=-1) virtual
Displays a critical message dialog box with a caption of caption and a text of 1, 2, and 3 buttons respectively. Returns the number of buttons that were clicked (0, 1, or 2).
Icon Qmessagebox::icon () const
Returns the icon for the message box
Const QPIXMAP *qmessagebox::iconpixmap () const
Returns the current icon
int qmessagebox::information (qwidget *parent,const QString &caption,const QString &text,int button0,int Button1=0,int button2=0) Virtual
Open a message box titled caption and text, click the Back button to identify Qmessagebox::ok/qmessagebox::no, and so on void qmessagebox::setbuttontext (int button, Const QString &text)
Sets the message box button's text to
void Qmessagebox::seticon (icon icon)
Set the icon for a message box
void Qmessagebox::seticonpixmap (const QPIXMAP &)
Set Current icon
void Qmessagebox::settext (const QString &)
Set message box text to be displayed
void Qmessagebox::settextformat (TextFormat)
Format the text displayed in a message box
QString Qmessagebox::text () const
Returns the text of the message box being displayed
TextFormat Qmessagebox::textformat () const
Returns the format of the text displayed in a message box
int qmessagebox::warning (qwidget *parent,const QString &caption,const QString &text,int button0,int button1, int button2=0) static
Open a warning message box titled caption text
If the parent 0 message box becomes the application Global modal dialog box
If the parent is a window part, the message box becomes a modal dialog box relative to the parent
============================================================================================
Here are a few dialog boxes:
First look at the most familiar qmessagebox::information. We used this in the previous code: Qmessagebox::information (NULL, "Title", "Content", Qmessagebox::yes | Qmessagebox::no, Qmessagebox::yes); The following is a simple example:
If we want to customize the picture, it is also very simple. It is not possible to use these static functions at this time, but rather we define a qmessagebox to use: Qmessagebox message (Qmessagebox::noicon, "Title", "Content with icon. ");
Message.seticonpixmap (Qpixmap ("Icon.png"));
Message.exec (); Here we are using the EXEC () function instead of show (), because this is a modal dialog that needs to have its own event loop, otherwise our dialog will flash through OH (Thanks to Laetitia Reminder).
It is important to note that, like other programs, the relative paths that we define in the program are relative to the address of the. exe file at run time. For example, we write "icon.png", which means to look for a "icon.png" file in the current directory of the. exe. This program works as follows:
============================================================================================
There is also a common question: how can we change the buttons in the Qmessagebox dialog box to Chinese:
How to display Chinese in 1,QT.
Qtextcodec*pcodec=qtextcodec::codecforname ("system");//Get System font encoding
Qtextcodec::setcodecforlocale (PCODEC);
Qtextcodec::setcodecforcstrings (PCODEC);
QTEXTCODEC::SETCODECFORTR (PCODEC);
If your operating system is currently in Chinese, you can display Chinese.
Or: #include <QtCore/QTextCodec>
QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname (Qtextcodec::codecforlocale ()->name ());
Qtextcodec::setcodecforlocale (Qtextcodec::codecforname (Qtextcodec::codecforlocale ()->name ());
Qtextcodec::setcodecforcstrings (Qtextcodec::codecforname (Qtextcodec::codecforlocale ()->name ());
2, the 1th step of the operation can be displayed in the text in Chinese, but not in the built-in Qmessagebox button above the display of Chinese.
What to do about it.
(1) Copy the file "Qt_zh_cn.qm" from the QT installation directory to your project directory.
For example, I am a "qt_zh_cn.qm" file that was copied from the "D:\QtSDK\Desktop\Qt\4.7.3\msvc2008\translations" directory.
Your project directory refers to the directory where your project's. Pro file resides.
(2) Create a new text file in the project directory and enter the following:
<RCC>
<qresource prefix= "/" >
<file>qt_zh_CN.qm</file>
</qresource>
</RCC>
After saving, change the suffix name of the text file from txt to QRC, indicating that this is a resource file.
(3) Add the above files to your project in Qt creator.
(4) Modify the code as follows, indicating that you want to load the appropriate resource file.
Qtranslator Otranslator;
Otranslator.load (":/qt_zh_cn"); Note that the string begins with ":/", followed by the name of the QM file that was just copied
Qapplication oapp (argc, argv);
Oapp.installtranslator (&otranslator);
(5) Recompile, run the program.
Or: (1) Ibid.
(2) Load the file: Qtranslator otranslator;
Otranslator.load ("QT_ZH_CN.QM");
Qapplication oapp (argc, argv);
Oapp.installtranslator (&otranslator);
It is better to load the file, the first to determine whether the file exists: QFile files (QT_ZH_CN.QM); bool B = file.exists ();
(3) Recompile, run the program.
The effect is as follows