Results 1 to 9 of 9

Thread: Background image for main window widget using css.

  1. #1
    Join Date
    Aug 2007
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation Background image for main window widget using css.

    Hi. I`m having a problem with setting the background-image css attribute for my main window wich is a subclasses Widget built with the designer.

    The main issue is that I use a MultiPageWidget and when I try to apply a background-image to the main window containing the multipagewidget, nothing happens.

    If, on the other hand, I apply a background-color to the main window, it works.

    Any help would be very much appreciated.

    Thank you.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Background image for main window widget using css.

    Exactly how do you set these background images and colors? Do you embed the background image as a resource or could it be a problem with relative paths? And what is a MultiPageWidget?
    J-P Nurmi

  3. #3
    Join Date
    Aug 2007
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11

    Lightbulb Re: Background image for main window widget using css.

    Ok, so I have the background image in a resource file.

    I have a widget as main window and on top of that, a MultiPageWidget. ( all built with the designer ).

    Now, using a stylesheet I haven't managed to set the window ( widget ) background so that the surrounding multipagewidget`s area ( the visible place belonging to the main window widget that is behind ) would have the specified background-image.

    After trying all the possibilities, I found a workaround... actually not a workaround... just something to make it work :P and it involves the following code, and not css ( what I wanted ).

    Qt Code:
    1. QPixmap bg(":/images/background.png");
    2. QPalette p(palette());
    3. p.setBrush(QPalette::Background, bg);
    4. setAutoFillBackground(true);
    5. setPalette(p);
    To copy to clipboard, switch view to plain text mode 

    This is quite a standard way of setting an image for background, but I wanted the clean css way, not the coded way...

    p.s.: This way It had a nice side-effect by displaying the background image trough the other controls like QGroupBox and QLabel that were contained in the MultiPageWidget. I expected them being opaque instead of transparent, but that will do.

    The CSS I used in trying to set the background image was simple:
    Qt Code:
    1. MainWindow {
    2. background-image: url(:/images/background.png);
    3. }
    To copy to clipboard, switch view to plain text mode 

    If I use a background-color: red; for example... I get the desired effect, only with a color, not an image.

    Hope I was explicit enough.

    Any ideeas?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Background image for main window widget using css.

    Does the class declaration of MainWindow contain Q_OBJECT macro? Could you paste the .qrc file?
    J-P Nurmi

  5. #5
    Join Date
    Aug 2007
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Background image for main window widget using css.

    Qt Code:
    1. class MainWindow : public QWidget, private Ui::MainWindowUi{
    2. Q_OBJECT
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. <RCC>
    2. <qresource>
    3. ...
    4. <file>images/background.png</file>
    5. ...
    6. </qresource>
    7. </RCC>
    To copy to clipboard, switch view to plain text mode 
    I already said it worked using the pixmap so that means that the resource is ok.


  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Background image for main window widget using css.

    I think you might have run into this problem:
    http://trolltech.com/developer/task-...ntry&id=166742
    J-P Nurmi

  7. #7
    Join Date
    Aug 2007
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation Re: Background image for main window widget using css.

    2007-06-12 17:48 - Entry created: Task status changed to 'Open', Resolution set to 'Pending', Version found set to '4.3.0', Version for fix set to '4.3.1', Priority changed to '1 - Highest'
    2007-06-13 17:17 - Task status changed to 'Active'
    2007-06-14 15:11 - Resolution set to 'Fixed'
    2007-06-14 15:11 - Task status changed to 'Closed'

    I use Qt 4.3.1. No fix.

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Background image for main window widget using css.

    Could you try what happens if you provide such paintEvent() as suggested in the "resolution"? I'm looking at qwidget.cpp of Qt 4.3.1 and see and empty paintEvent().
    J-P Nurmi

  9. #9
    Join Date
    Aug 2007
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11

    Lightbulb Re: Background image for main window widget using css.

    Tried it and worked.

    Thumbs up!

Similar Threads

  1. Background image and semitransparent widget
    By asieriko in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2007, 17:43
  2. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 18:52
  3. Replies: 15
    Last Post: 23rd March 2007, 17:16
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 17:36

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.