Results 1 to 7 of 7

Thread: background colour

  1. #1
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question background colour

    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:
    Qt Code:
    1. QPalette p( this->palette() );
    2. p.setColor( QPalette::Window, QColor(128,50,50,255) );
    3. this->setPalette( p );
    To copy to clipboard, switch view to plain text mode 

    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
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: background colour

    Change the background colour of QScrollArea's viewport and call setAutoFillBackground( true ) (also on the viewport).

  3. The following user says thank you to jacek for this useful post:

    kw (1st April 2006)

  4. #3
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: background colour

    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

  5. #4
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: background colour

    Quote Originally Posted by kw
    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.
    Save yourself some pain. Learn C++ before learning Qt.

  6. The following user says thank you to Chicken Blood Machine for this useful post:

    kw (1st April 2006)

  7. #5
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: background colour

    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

  8. #6
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: background colour

    For anyone interested, I found out there's an easier and much neater way to do this..
    After the:
    Qt Code:
    1. QPalette p( this->palette() );
    2.  
    3. p.setColor( QPalette::Window, QColor(128,50,50,255) );
    4.  
    5. this->setPalette( p );
    To copy to clipboard, switch view to plain text mode 

    Just call
    Qt Code:
    1. this->setBackgroundRole(QPalette::Window);
    To copy to clipboard, switch view to plain text mode 

    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

  9. #7
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: background colour

    Cool! I'll make a note of that.
    Save yourself some pain. Learn C++ before learning Qt.

Similar Threads

  1. QLCDNumber with transparent background?
    By PolyVox in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2008, 06:34
  2. Replies: 5
    Last Post: 30th March 2008, 17:53
  3. Dynamically changing QLabel background colour
    By T4ng10r in forum Qt Programming
    Replies: 19
    Last Post: 19th April 2007, 13:47
  4. Replies: 3
    Last Post: 8th December 2006, 19:51
  5. Replies: 1
    Last Post: 5th April 2006, 17:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.