简体   繁体   中英

Qt statusbar color

I'm using Qt with Python, and I've got a mainwindow with a status bar at the bottom. I can display a message in the bar using a QLabel, and set the color of that message using something like "<font color=\\"green\\">In progress</font>" for the QLabel text.

I would like to also put a temporary message in the status bar, and assign a color to that message as well. However since it's not a QLabel this time (I'm using QStatusBar::showMessage which just takes a QString) I can't change the color anymore. The tags above are not recognized and the entire string "<font color=\\"green\\">In progress</font>" is shown in gray.

Does anyone have any ideas?

To set the background or text color for a QStatusBar, change it's styleSheet before showing the message:

    self.status.setStyleSheet("QStatusBar{padding-left:8px;background:rgba(255,0,0,255);color:black;font-weight:bold;}")
    self.status.showMessage("Error Cannot determine filepath", msecs= 5000)

on init , connect the QStatusBar's messageChanged(QString) to a statusChanged() function.

    def statusChanged(self, args):
        '''If there are no arguments (the message is being removed) 
        change the background back to transparent/ text back to black'''
        if not args:
            self.status.setStyleSheet("QStatusBar{padding-left:8px;background:rgba(0,0,0,0);color:black;font-weight:bold;}") 

T

Unfortunately, QStatusBar::showMessage() doesn't support rich text formatting . This was even reported as a feature request long time ago, but it seems it didn't get enough attention.

I think your best bet is to either stick with plain text messages or manipulate your existing QLabel directly. This would require some additional work to handle temporary status changes, so it's your call to decide if it's worth the trouble.

如果你的showMessages文本都是同一种颜色,你可以通过QtDesigner(window text color)在QStatusBar的调色板中定义临时消息,然后使用QLabel颜色来表示不同颜色的普通消息和永久消息。

The shortest solution I could find for this problem so far:

    ui->statusBar->setStyleSheet("color: red");
    ui->statusBar->showMessage("Your error message", 2000);
    QTimer::singleShot(2000, [this]{ ui->statusBar->setStyleSheet("color: black"); }); 

It's not 100% clean though - if another message of this kind is triggered during the 2 seconds of the timer run time, then the color possibly changes back too early. But in practice this will hardly be of any relevance.

You can also subclass QStatusBar and implemented "colored" status messages, something like (in C++):

class QStatusBarX : public QStatusBar
{
  public:

  QStatusBarX::QStatusBarX(QWidget * parent = 0)
  {
  }

  QStatusBarX::~QStatusBarX(void)
  {
  }

  void showMessageGreen(const QString & message)
  {
    this->setStyleSheet("color: green");
    this->showMessage(message);
  }
};

Python 中使用

self.statusBar().setStyleSheet("color : pink") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM