qt - 如何在 QStatusBar 中获取彩色瞬时消息?

标签 qt colors message qstatusbar

我需要对 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/

相关文章:

c++ - 配置 Qt .pro 文件以使用 cl.exe 和 link.exe 修复链接错误

c++ - QT 4.5 - QGraphicsItem 分配给 0x0 的父场景

Qt HTTP 服务器?

c++ - 消息的所有权,使用哪种设计模式?

mysql - 两个表之间的数据库消息系统

c++ - 手动调用 connectSlotsByName()

java - Robot 的 getPixelColor(int x, int y) 方法如何工作?

50% 不透明度的 CSS 2 颜色 == 其他颜色

colors - 如何在 Dreamweaver CS5 (Windows) 中将代码配色方案重置为默认值?

java - 分析驻留在队列 RabbitMQ 中的消息