我需要对 Qt5.5 应用程序状态栏中闪烁的消息进行颜色编码。
我是这样使用 showMessage 的:
ui->statusBar->showMessage("My Message", 5000);
我想更改单个消息的颜色。 我发现除了子类化 QStatusBar 和覆盖 showMessage() 之外别无他法。 我真的需要这样的侵入性改变吗?
我尝试按照以下方式使用富文本:
ui->statusBar->showMessage(QString("<html><head/><body><p style=\"color:red\">%1</p></body></html>").arg("My Message"));
但它似乎没有被识别(打印标签)。
改变调色板或设置样式表将不限于当前消息。
我还能尝试什么?
最佳答案
如何在 QStatusBar 中获取彩色 transient 消息?
Altering the palette or setting the style-sheet will not be limited to the current message.
更改样式表不一定适用于程序中的所有小部件。您绝对可以限制样式表的范围。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// the stylesheet can be applied within ui->statusBar hierarchy of
// objects, but you can make it even more narrow scope as
// ui->statusBar->setStyleSheet("QStatusBar{color:red}") if needed
ui->statusBar->setStyleSheet("color: red");
ui->statusBar->showMessage("Text!");
QTimer::singleShot(2000, this, SLOT(tryAnotherColor()));
}
void MainWindow::tryAnotherColor()
{
ui->statusBar->setStyleSheet("color: blue");
ui->statusBar->showMessage("More Text!");
}
I tried to use Rich Text
我的猜测是,并非所有 Qt 小部件控件都具有富文本呈现功能,但大多数都理解类似 CSS 的样式表。
关于qt - 如何在 QStatusBar 中获取彩色瞬时消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35247835/