Qt statusbar color

10,094

Solution 1

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.

Solution 2

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

Solution 3

If your showMessages text will be all of the same color, you can define it in the palette of QStatusBar via QtDesigner(window text color) for temporary messages, and then use the QLabel color for normal and permanent messages of different colors.

Solution 4

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.

Share:
10,094
Admin
Author by

Admin

Updated on June 07, 2022

Comments

  • Admin
    Admin about 2 years

    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?

  • Admin
    Admin about 13 years
    Thanks Karol, helpful to know that it's simply not directly possible with Qt. In case it helps someone else, I came up with another solution. I added a QLabel to the statusbar using AddWidget, then created a timer that would periodically clear that label. All I had to do after that was make sure that timer was reset any time the QLabel was updated with new text.
  • Karol J. Piczak
    Karol J. Piczak about 13 years
    Good to hear you found a way. Qt is like that, there are some limitations here and there, but most of the time you can find a workaround. At least that was my impression back with 4.3.
  • Admin
    Admin over 8 years
    That is awesome! In Qt Designer, click on the window somewhere so that it's the current object, go to palette about 1/3 the way down in the Property Editor and "window text" is right at the top. Will also effect subwidgets such as the tab names in a tab widget that you have added to the window in Qt Designer. But it doesn't seem to effect every widget you add programatically which is good. If it does and that's not desired, then override the color with the subwidget's stylesheet.