PDA

View Full Version : background colour



kw
31st March 2006, 19:08
Hi,

I have a QImage inside a QScrollArea in my QMainWindow.
Now that the classes have been mentioned, let me tell you the actual story. I'm custom drawing something that I want to display in a window. I'm using a QImage for this, because I will need pixel level access. I've put this QImage inside a scroll area so that if the picture Im drawing is larger than the window (or even the screen), the whole thing can still be viewed by scrolling. This works.
The problem is that when you resize the window to be *larger* than the image I'm drawing, it fills everything outside my QImage with the standard grey background.
After some messing around figuring out what background I was actually seeing, I ended up setting a new background colour for my QMainWindow, as follows:


QPalette p( this->palette() );
p.setColor( QPalette::Window, QColor(128,50,50,255) );
this->setPalette( p );


This works.. in a way.. The problem is that it's not just using this colour as the background for the window, but also as the status bar colour, the scrollbars are tinted with it, and the corner of the scrollbars is filled with the colour. All in all, not a very nice look.
Is there any way I can paint *just* the background of my window?

Thanks for any pointers you can give me..
I've attached a screenshot with clearly distinguishable colours. Black is the QImage and blue the window background.

Regards,
kw

jacek
31st March 2006, 19:19
Change the background colour of QScrollArea's viewport and call setAutoFillBackground( true ) (also on the viewport).

kw
31st March 2006, 23:10
That's great :-)

The status bar now retains its original colour, and it looks a lot better.
The only part that's left in the background colour that still looks a bit odd is the corner where the scrollbars meet. I'm not sure that's fixable though, this seems like the right way to do things.. (of course if anyone knows a way, I can't wait to hear it ;-))

Regards,
kw

Chicken Blood Machine
1st April 2006, 01:24
That's great :-)

The status bar now retains its original colour, and it looks a lot better.
The only part that's left in the background colour that still looks a bit odd is the corner where the scrollbars meet. I'm not sure that's fixable though, this seems like the right way to do things.. (of course if anyone knows a way, I can't wait to hear it ;-))

Regards,
kw

It's a bit hacky, but you can draw a square in the lower right-hand corner of the scroll view. Fill it with the correct colour from the palette (Qt::Window or Qt::Button I think). You can calculate the dimensions of the square, by getting the width of one of the scrollbars.

Qt3 scrollview used to have a setCornerWidget() method which would have made this easier.

kw
1st April 2006, 02:07
awesome :)

Granted, it's a bit hackish indeed.. but when other options fail, it can work to just paint the square of (scrollbar_width, scrollbar_height) in that corner in the standard button colour.
It'd look nicer anyway. I hope the trolltech people will change this actually, I would much prefer to have this corner have the same colour as the buttons (like the up/down/left/right arrows of the real scrollbars), I think this is the standard way of win and nix anyway (not sure about mac)..

-kw

kw
11th April 2006, 00:34
For anyone interested, I found out there's an easier and much neater way to do this..
After the:

QPalette p( this->palette() );

p.setColor( QPalette::Window, QColor(128,50,50,255) );

this->setPalette( p );

Just call

this->setBackgroundRole(QPalette::Window);

All of this code is from within the qscrollarea widget (subclass it, or change the code a bit so it doesn't use 'this' and do it from the outside)

regards,
kw

Chicken Blood Machine
11th April 2006, 00:44
Cool! I'll make a note of that.