1.代码:
QMessageBox ( Icon icon, const QString & title, const QString & text, StandardButtons buttons = NoButton, QWidget * parent = 0, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint ) |
2.其中:Icon icon为消息的类型,可以按F1查看Warning中的其它类型,共有如下: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. |
const QString & title为消息窗口标题字符串;
const QString & text为消息要显示的内容,可以用HTML标签来写,这样可以设置字体的大小颜色等。
StandardButtons buttons = NoButton为要显示的按键类型,可以按F1查看按键的类型。
QWidget * parent = 0 要显示的父窗口,可以用this或NULL代替。
Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint 为窗体的类型,可以按F1查看有哪几种类型。
3.示例:
QMessageBox message(QMessageBox::Warning,"Show Qt","<font size='26' color='red'>Do you want to show Qt dialog?</font>", QMessageBox::Yes | QMessageBox::No, NULL);
message.exec(); //阻塞显示出来。
这里介绍下HTML格式(其实可以上网查看):
"<font size='26' color='red'>Do you want to show Qt dialog?</font>"
其中
<font size='26' color='red'>为设置字体的大小为26,颜色为红色,显示的内容为:
Do you want to show Qt dialog?
举几个例子如下:
(1) 作为标题:
用一个QLabel对象,使其字体加大、加粗、居中,使用如下三个标签即可。
new QLabel("<font size='+1'><b><p align='center'>标题</p></b></font>", this, "title")(2) 加下划线:
使用<u></u>即可实现。(3) 给局部加样式
还可以对text的部分内容添加标签,以使个别内容使用不同字体、样式,并且不影响整体字体。如,给字加颜色、大小、字体等。
<font color='#5500ff' size='+1' face='Sans'>被设置了字体</font>(4) 画横线
4、其实对于HTML可以在网上找到对应的转换工具,只需要将相应的文字输入点转换即可,然后将代码复制过来即可,但应注意将其中的换行符去掉,否则会出现编译错误,另将双引号全部变成单引号,这样才可填进去,如:hello,world!
使用<hr>即可实现。HTML脚本转换后的代码为:<p> he<span style="color: rgb(0, 176, 80);">ll</span>o,<span style="color: rgb(255, 0, 0); font-size: 24px;">w</span>orld! </p>则QT中的消息中应填入:QMessageBox::warning(this,tr("warning"),tr("<p>he<span style='color: rgb(0, 176, 80);'>ll</span>o,<span style='color: rgb(255, 0, 0); font-size: 24px;'>w</span>orld!</p>"),QMessageBox::Yes);将换行符及双引号都转换掉才正确。
只有支持HTML脚本的文本才可用HTML,其它不可用。