Qt Statusbar Color
Solution 1:
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.
defstatusChanged(self, args):
'''If there are no arguments (the message is being removed)
change the background back to transparent/ text back to black'''ifnot args:
self.status.setStyleSheet("QStatusBar{padding-left:8px;background:rgba(0,0,0,0);color:black;font-weight:bold;}")
T
Solution 2:
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 existingQLabel
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 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.
Solution 5:
You can also subclass QStatusBar
and implemented "colored" status messages, something like (in C++):
classQStatusBarX : public QStatusBar
{
public:
QStatusBarX::QStatusBarX(QWidget * parent = 0)
{
}
QStatusBarX::~QStatusBarX(void)
{
}
voidshowMessageGreen(const QString & message)
{
this->setStyleSheet("color: green");
this->showMessage(message);
}
};
Post a Comment for "Qt Statusbar Color"