有时候,除了文字,还需要在QTextBrowser显示一些图片,图标之类的。语法实际上也很简单,用QTextBrowser的insertHtml(QString)就行。
理论上下面这样的代码就可以了
QString html = "<div style=\"height=20\"> \n"
"<img src=:/images/message/warning.svg style=\"vertical-align:middle\"> \n"
"<span>TestTestTestTest</span> \n"
"</div>";
ui->txb->insertHtml(html);
或者这样的代码
QString html = "<div style = \"height=10\"> "
"<img height = 20 width = 20 src = :/images/message/warning.svg> <span>TestTestTest</span> "
"</div>";
ui->txb->insertHtml(html);
但是经过实际测试,这些代码都不行,或者其他的html代码,各种都试过了,呈现的无非是下面两种效果,要么不对齐,要么图标变大了还是不对齐。
最后用table解决
QString html = "<table>"
"<tr>"
"<td><img height=\"24\" src=:/images/message/warning.svg></td>"
"<td style=\"vertical-align: middle;\">Here is some text.</td>"
"</tr>"
"</table>";
加上时间和文字,就能当信息输出框用了
void MessageOutputBox::appendWarningMsg(const QString &str)
{
QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
QString html = "<table>"
"<tr>"
"<td><img height=\"20\" src=:/images/message/warning.svg></td>"
"<td style=\"vertical-align: middle; font-size:14px\"> %1 </td>"
"<td style=\"vertical-align: middle; font-size:14px; color:#F9D65D\"> %2 </td>"
"</tr>"
"</table>";
ui->txb_warning->insertHtml(html.arg(time, str));
ui->txb_message->insertHtml(html.arg(time, str));
}