PDA

View Full Version : Problems setting a background image



franco.amato
28th January 2011, 20:22
Hi to all,
I have some problems setting a background image on a QWidget.
I tried with the designer and also with code so:


ImageRenderer::ImageRenderer(QWidget* parent)
: QWidget(parent),
m_qImage(NULL)
{
setFixedSize( QSize(640, 480) );
setStyleSheet("background-image: url(:/resources/images/logo.jpg);");

//m_image = new QPixmap();
m_qImageBuffer = (uchar *)malloc(640 * 480 * sizeof(uchar));
}

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

Lykurg
28th January 2011, 20:29
Sure you have the uri right? what is the output of
QPixmap p(":/resources/images/logo.jpg");
qWarning() << p.isNull();Or you don't have loaded the jpeg support. Try to set a png image for testing.

wysota
28th January 2011, 20:30
Did you reimplement the paint event for your widget? Does the class contain Q_OBJECT macro?

franco.amato
28th January 2011, 20:43
it returns true but is really strange, the image is there


Did you reimplement the paint event for your widget? Does the class contain Q_OBJECT macro?

Hi Wysota yes this is the class:


class ImageRenderer : public QWidget
{
Q_OBJECT

public:
ImageRenderer(QWidget* parent=0);
~ImageRenderer();

void setImage(IplImage*);

public slots:
QImage IplImageToQImage(const IplImage* iplImage);

protected:
/* paint event */
virtual void paintEvent( QPaintEvent* event );

private:
QPixmap m_pixmap;
IplImage* m_frame;

/* OpenCV -> Qt */
QImage* m_qImage;
unsigned char* m_qImageBuffer;
};

#endif //#ifndef _IMAGERENDERER_H_


Sure you have the uri right? what is the output of
QPixmap p(":/resources/images/logo.jpg");
qWarning() << p.isNull();Or you don't have loaded the jpeg support. Try to set a png image for testing.

I also converted to png and nothing

Lykurg
28th January 2011, 20:54
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?

wysota
28th January 2011, 20:58
So how did you implement paintEvent?

franco.amato
28th January 2011, 21:23
So how did you implement paintEvent?

At the moment the paintEvent is empty it should contains something using the stylesheet in the ctor?

wysota
28th January 2011, 21:25
If it is empty then whatever you do (even if you get jpgs to work as Lykurg pointed out) your background won't appear.

franco.amato
28th January 2011, 21:33
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:


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:


void ImageRenderer::paintEvent( QPaintEvent* pe )
{
QPainter p(this);
p.setRenderHint( QPainter::Antialiasing, true );

// image
p.drawPixmap( 0, 0, m_pixmap );
}

and the ctor so:


ImageRenderer::ImageRenderer(QWidget* parent)
: QWidget(parent),
m_qImage(NULL)
{
setFixedSize( QSize(640, 480) );

m_pixmap.load(":/CertiIris/images/logo.png");
}


and now I can see the background image.
So my question is: where I can use styleSheet and where not?
Regards

wysota
28th January 2011, 22:31
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.

franco.amato
28th January 2011, 22:33
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