如何在QStatusBar中获取彩色瞬态消息



我需要对Qt5.5应用程序状态栏中闪烁的消息进行颜色编码。

我正在使用这样的显示消息:

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中获取彩色瞬态消息?

更改调色板或设置样式表将不限于当前 消息。

更改样式表不一定适用于程序中的所有小部件。您绝对可以限制样式表的范围。

#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!");
}

我尝试使用富文本

我的猜测是,并非所有Qt小部件控件都具有富文本渲染功能,但大多数控件都理解类似CSS的样式表。

最新更新