Results 1 to 4 of 4

Thread: How to properly set a background image of a custom widget

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to properly set a background image of a custom widget

    Hi forum,
    I have a custom widget, showing the output of a video camera ( after some calculations ) and I would display a background png image.
    I tried with the
    Qt Code:
    1. myWidget->setStyleSheet("background-image: url(:/screen.png);");
    To copy to clipboard, switch view to plain text mode 
    with no results.
    Should i set it manually in the paint event?
    Regards
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to properly set a background image of a custom widget

    Should i set it manually in the paint event?
    Is the path to the resource correct? (You can test by trying to load in code: QImage::load()).

    You can try drawing it in the paint event (using QPainter::drawImage()). Don't load the image in the paintEvent(), load it in your widget constructor and store it as a member variable.

    Of course, if your video frame is the same size as the window, setting the background image won't have any effect because the video frame will overwrite it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to properly set a background image of a custom widget

    Hi,
    yes the path is correct.
    I would like to display the bg image only while I am not receiving frames from the camera.
    Thanx
    Franco Amato

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to properly set a background image of a custom widget

    I would like to display the bg image only while I am not receiving frames from the camera.
    The I think the only way to do that is to paint it manually in the paintEvent(). The stylesheet doesn't know anything about your application's details, so your paint event has to decide whether to show the background image or the current frame.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    franco.amato (26th January 2021)

Similar Threads

  1. Properly adding custom widget into Qt Designer
    By Baltharos in forum Newbie
    Replies: 31
    Last Post: 29th March 2015, 23:48
  2. Background-image in top level widget
    By alitoh in forum Qt Programming
    Replies: 7
    Last Post: 2nd November 2012, 04:32
  3. Custom widget won't size properly in a table cell
    By zeroknowledge in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2011, 17:32
  4. Replies: 10
    Last Post: 10th November 2010, 04:12
  5. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 14:12

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.