QT的QMessageBox使用方法(设置字体大小,颜色等)

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:

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.
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) 画横线
使用<hr>即可实现。

4、其实对于HTML可以在网上找到对应的转换工具,只需要将相应的文字输入点转换即可,然后将代码复制过来即可,但应注意将其中的换行符去掉,否则会出现编译错误,另将双引号全部变成单引号,这样才可填进去,如:hello,world!
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,其它不可用。










                
  • 7
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值