PDA

View Full Version : cannot change QStatusBar background color



cic
18th November 2013, 13:25
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QMainWindow window;
QStatusBar statusBar;
QPalette palette;

window.setWindowTitle("QMainWindow with Colored QStatusBar");
window.setFixedSize(300,100);
window.setStatusBar(&statusBar);

palette.setColor(QPalette::Background, Qt::red);

statusBar.setPalette(palette);
statusBar.showMessage("Red QStatusBar");
window.show();

return app.exec();
}


i used the example code but the background color remained as the default one.
any idea?

9799

anda_skoa
18th November 2013, 16:21
Usually the code to change a widget's background color looks like this


QPalette palette = widget->palette(); // get current palette

palette.setColor(QPalette::Background, color); // modify palette

widget->setPalette(palette); // apply modified palette

widget->setAutoFillBackground(true); // tell widget to fill its background itself


Btw, you don't have to create a QStatusBar, QMainWindow::statusBar() will do that for you and always return the same instance

Cheers,
_