Results 1 to 8 of 8

Thread: Applying a backgroung image to a widget

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Applying a backgroung image to a widget

    Hello!
    I apologize to disturb you. I am facing a problem of setting a background image to my widget.
    I have applied the following function, but the widgest has turned to black completely:
    QPalette palette;
    QString appDir = QApplication::applicationDirPath();
    palette.setBrush(this->backgroundRole(),QBrush(QImage(appDir +"/Background.png")));
    this->setPalette(palette);
    Does anyone has a idea how to solve the problem?
    I will be greatfull to everyone who would help me solving this problem:
    -either by telling me why the widget has turned to black
    -or by recommending me aonther function.
    Many thanks in advance.

  2. #2
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Applying a backgroung image to a widget

    Try to use QPalette::Window as a color role.

  3. #3
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Applying a backgroung image to a widget

    Thanks!
    I have a picture from my desktop and it is this picture that I wanted to apply as background.
    QPalette::Window is not working in my case.
    Thanks anyway!

  4. #4
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Applying a backgroung image to a widget

    You mean , you're still having the same result ? Are you sure your path is correct (try smth. like "D:/image.png") ? It's weird because it works just fine for me.

  5. #5
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Applying a backgroung image to a widget

    Hallo AlexSudnik. The matter is that my window turns completely black when I apply the image, so that the characters dissapear. If there is a solution to overcome the black color, the whole problem will be solve. Do you have an idea how to get it right please?

  6. #6
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Applying a backgroung image to a widget

    It looks like the problem is in the image's path (or your image has different name ) : i've moved the image from the folder and...as you say widget's background has turned black.Try to check the file's name spelling or this:

    palette.setBrush( QPalette::Window , QBrush(QImage( "Background.png" ) ) );

    and make sure that Background.png is placed where an executable is.

    It must work , if it's not...share the code.

  7. #7
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Applying a backgroung image to a widget

    Hello AlexSudnik!
    I am greatfull to you for your help. It is fine now. I have followed your advise and moved the image where the executable is. It is working now.
    To sumarize, the widget has turned black because the image was separated from the executable in another folder.
    One more time many thanks for your help.

  8. #8
    Join Date
    Feb 2012
    Posts
    27
    Thanked 2 Times in 2 Posts

    Default Re: Applying a backgroung image to a widget

    Use stylesheet to apply background image to a widget...

Similar Threads

  1. Replies: 2
    Last Post: 4th November 2016, 09:12
  2. Applying tr() function
    By prashant.patel in forum Newbie
    Replies: 4
    Last Post: 7th September 2011, 22:48
  3. Add color to ui backgroung
    By maarvi in forum Newbie
    Replies: 7
    Last Post: 15th June 2011, 08:05
  4. Replies: 0
    Last Post: 19th January 2011, 12:49
  5. Problem applying setWindowOpacity to a custom Widget
    By yellowmat in forum Qt Programming
    Replies: 8
    Last Post: 1st November 2006, 10:05

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
  •  
Qt is a trademark of The Qt Company.