PDA

View Full Version : Applying a consistent background



KanadaKid
27th December 2009, 06:48
Hey,

I'm using Qt4 on Mac OS X. I'm trying to set an image as the background for my main window, and also I want that image to show through all the child widgets so it creates a uniform background. Basically, I want for all child widgets in the main window to have transparent backgrounds, so that the main window's background image shows through. I tried the following:

// called from constructor of my QMainWindow-derived class
setStyleSheet("background-image: url(some_bg.png)");

This basically works, however, EVERY child widget attempts to set this image as its own background. That is, instead of having one, uniform background, I get several partial images as each widget uses that image as its own background. Such as a QLabel, which for some reason draws its normally gray background using that same image. How can I achieve the aforementioned results, where the child widgets let the main window background image simply show through?

Thanks!

numbat
27th December 2009, 09:36
Remember, style sheets are cascading (the C in CSS) so child controls will get the style applied to a parent. I think something like this may work:


*
{
background-color: rgba(255, 255, 255, 10%);
}
QMainWindow
{
background-image: url(some_bg.png);
background-color: gray;
}

We use the * selector to set the background color of all widgets to (almost) transparent white. We then use the QMainWindow selector to set the background image on QMainWindow derived classes only.

Coises
27th December 2009, 18:56
Try something like:

setStyleSheet("#centralWidget {background-image: url(some_bg.png);} #centralWidget * {background: transparent;}");