QT的QMessageBox使用方法(设置字体大小,颜色等) | 您所在的位置:网站首页 › qt设置窗口标题背景颜色 › 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: 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","Do you want to show Qt dialog?", QMessageBox::Yes | QMessageBox::No, NULL); message.exec(); //阻塞显示出来。 这里介绍下HTML格式(其实可以上网查看): "Do you want to show Qt dialog?" 其中 为设置字体的大小为26,颜色为红色,显示的内容为: Do you want to show Qt dialog? 举几个例子如下: (1) 作为标题: 用一个QLabel对象,使其字体加大、加粗、居中,使用如下三个标签即可。 new QLabel(" 标题 ", this, "title")(2) 加下划线: 使用即可实现。 (3) 给局部加样式 还可以对text的部分内容添加标签,以使个别内容使用不同字体、样式,并且不影响整体字体。如,给字加颜色、大小、字体等。 被设置了字体 (4) 画横线 使用即可实现。 4、其实对于HTML可以在网上找到对应的转换工具,只需要将相应的文字输入点转换即可,然后将代码复制过来即可,但应注意将其中的换行符去掉,否则会出现编译错误,另将双引号全部变成单引号,这样才可填进去,如:hello,world! HTML脚本转换后的代码为: hello,world! 则QT中的消息中应填入: QMessageBox::warning(this,tr("warning"),tr("hello,world! "),QMessageBox::Yes); 将换行符及双引号都转换掉才正确。只有支持HTML脚本的文本才可用HTML,其它不可用。 |
CopyRight 2018-2019 实验室设备网 版权所有 |