View Full Version : statusBar() message color change

8th August 2007, 00:00

I want to be able to change the color of the statusBar message that appears in my QMainWindow object based upon the type of message; normal, warning, error...

The problem I need help with is that I'm also using the timer function of the statusBar. This means that if I change the palette of the statusBar and display the message, I must wait until the timer elapses to change the palette back.

For example, I display a warning message (forces a palette change to blue). If, after I show the message, I change the palette back, the palette switching happens so fast that the string never appears in the warning color.

void MainWin::DisplayStatus( QString sStatus, QColor rgbColor, int duration )
if ( !sStatus.isEmpty() )
QPalette palette;
palette.setColor( QPalette::WindowText, rgbColor );
statusBar()->setPalette( palette );
statusBar()->showMessage( sStatus, duration );

// resets the palette before the message can be seen in the previous color
if ( rgbColor != COLOR_StatusNorm )
palette.setColor( QPalette::WindowText, COLOR_StatusNorm );
statusBar()->setPalette( palette );

If I don't reset the statusBar palette, all subsequent messages will use the warning color.

Most of the messages have a duration of 3-5 seconds so a Sleep() is not appropriate.

Does anyone have suggestions or am I trying to do something the statusBar was not intended for?

8th August 2007, 00:09
Why don't you use a singleshot timer with the same duration as the status message?
You start the timer right after you set a new status message.

Connect a slot to its timeout signal and in that slot change the color back.

Since the timer and the message have the same timeout, there should be a slight delay( the order of milliseconds) between the message timeout and timer timeout, but really, no one is going to notice that.


8th August 2007, 00:20
Thanks Marcel, that looks like just the solution I need.