Results 1 to 11 of 11

Thread: Problems setting a background image

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    615
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems setting a background image

    Hi to all,
    I have some problems setting a background image on a QWidget.
    I tried with the designer and also with code so:

    Qt Code:
    1. ImageRenderer::ImageRenderer(QWidget* parent)
    2. : QWidget(parent),
    3. m_qImage(NULL)
    4. {
    5. setFixedSize( QSize(640, 480) );
    6. setStyleSheet("background-image: url(:/resources/images/logo.jpg);");
    7.  
    8. //m_image = new QPixmap();
    9. m_qImageBuffer = (uchar *)malloc(640 * 480 * sizeof(uchar));
    10. }
    To copy to clipboard, switch view to plain text mode 

    The image is correctly in the resouces.
    Where am I wrong?
    Regards
    Franco Amato

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems setting a background image

    Sure you have the uri right? what is the output of
    Qt Code:
    1. QPixmap p(":/resources/images/logo.jpg");
    2. qWarning() << p.isNull();
    To copy to clipboard, switch view to plain text mode 
    Or you don't have loaded the jpeg support. Try to set a png image for testing.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems setting a background image

    Did you reimplement the paint event for your widget? Does the class contain Q_OBJECT macro?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    615
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems setting a background image

    it returns true but is really strange, the image is there

    Quote Originally Posted by wysota View Post
    Did you reimplement the paint event for your widget? Does the class contain Q_OBJECT macro?
    Hi Wysota yes this is the class:

    Qt Code:
    1. class ImageRenderer : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ImageRenderer(QWidget* parent=0);
    7. ~ImageRenderer();
    8.  
    9. void setImage(IplImage*);
    10.  
    11. public slots:
    12. QImage IplImageToQImage(const IplImage* iplImage);
    13.  
    14. protected:
    15. /* paint event */
    16. virtual void paintEvent( QPaintEvent* event );
    17.  
    18. private:
    19. QPixmap m_pixmap;
    20. IplImage* m_frame;
    21.  
    22. /* OpenCV -> Qt */
    23. QImage* m_qImage;
    24. unsigned char* m_qImageBuffer;
    25. };
    26.  
    27. #endif //#ifndef _IMAGERENDERER_H_
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by Lykurg View Post
    Sure you have the uri right? what is the output of
    Qt Code:
    1. QPixmap p(":/resources/images/logo.jpg");
    2. qWarning() << p.isNull();
    To copy to clipboard, switch view to plain text mode 
    Or you don't have loaded the jpeg support. Try to set a png image for testing.
    I also converted to png and nothing
    Franco Amato

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems setting a background image

    Quote Originally Posted by franco.amato View Post
    it returns true
    Well that's bad and means you havn't the right path or not installed the jpeg image plugin. How does your qrc look like and further how do you implement the paint event function?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems setting a background image

    So how did you implement paintEvent?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    615
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems setting a background image

    Quote Originally Posted by wysota View Post
    So how did you implement paintEvent?
    At the moment the paintEvent is empty it should contains something using the stylesheet in the ctor?
    Franco Amato

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems setting a background image

    If it is empty then whatever you do (even if you get jpgs to work as Lykurg pointed out) your background won't appear.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    615
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems setting a background image

    Quote Originally Posted by wysota View Post
    If it is empty then whatever you do (even if you get jpgs to work as Lykurg pointed out) your background won't appear.
    I converted the image in png and the url is correct, now it find the correct url but I still don't see the background.
    So what I have to write in the paintEvent?


    Added after 6 minutes:


    Quote Originally Posted by wysota View Post
    If it is empty then whatever you do (even if you get jpgs to work as Lykurg pointed out) your background won't appear.
    I changed the paintEvent so:
    Qt Code:
    1. void ImageRenderer::paintEvent( QPaintEvent* pe )
    2. {
    3. QPainter p(this);
    4. p.setRenderHint( QPainter::Antialiasing, true );
    5.  
    6. // image
    7. p.drawPixmap( 0, 0, m_pixmap );
    8. }
    To copy to clipboard, switch view to plain text mode 

    and the ctor so:

    Qt Code:
    1. ImageRenderer::ImageRenderer(QWidget* parent)
    2. : QWidget(parent),
    3. m_qImage(NULL)
    4. {
    5. setFixedSize( QSize(640, 480) );
    6.  
    7. m_pixmap.load(":/CertiIris/images/logo.png");
    8. }
    To copy to clipboard, switch view to plain text mode 

    and now I can see the background image.
    So my question is: where I can use styleSheet and where not?
    Regards
    Last edited by franco.amato; 28th January 2011 at 21:33.
    Franco Amato

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems setting a background image

    If you override the paint event and do custom painting then how do you expect to get stylesheets to work if they are part of the original painting routine?

    A description of what is needed for a bare QWidget to respect stylesheets was recently provided on the forum. Search for it if you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    615
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems setting a background image

    Quote Originally Posted by wysota View Post
    If you override the paint event and do custom painting then how do you expect to get stylesheets to work if they are part of the original painting routine?

    A description of what is needed for a bare QWidget to respect stylesheets was recently provided on the forum. Search for it if you want.
    Ok thank you
    Franco Amato

Similar Threads

  1. Replies: 1
    Last Post: 3rd July 2010, 11:17
  2. Setting QDial background image using stylesheet
    By dpatel in forum Qt Programming
    Replies: 0
    Last Post: 28th April 2010, 13:13
  3. Setting background image for Central Widget
    By xfurrier in forum Qt Programming
    Replies: 8
    Last Post: 25th February 2010, 10:14
  4. Replies: 0
    Last Post: 6th April 2009, 01:20
  5. Setting background image of QFrame
    By Claymore in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2007, 19:50

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.