Multiple images on QLabel [Solved]
-
Hello all,
I'm trying to display transparent image on QLabel. I also need this checkerred background for showing transparency. But, when I apply pixmap, the background image in the stylesheet is not showing up. The image has opacity. Can anyone explain why?
I have to show the semi transparent image on the checkered background. The qlabel size keeps varying according to the size of image.
@
imageLabel->setStyleSheet("background-image:url(:/checkered.png)");
imageLabel->setPixmap(image_pixmap);@ -
I suspect that behind the scenes the stylesheet and setPixmap use the same method to set an image for the QLabel therefore your second call simply supersedes your first. The easiest way to achieve what you want is probably to combine your two images into a single QPixmap and use that instead:
@
...
QLabel *label=new QLabel(this);QPixmap *pixmap=new QPixmap(WIDTH, HEIGHT);
pixmap->fill(Qt::transparent);
QPainter *painter=new QPainter(pixmap);
painter->drawPixmap(0, 0, WIDTH, HEIGHT, QPixmap(":/checkered.jpeg"));
painter->drawPixmap(0, 0, WIDTH, HEIGHT, QPixmap(":/my_image.png"));
painter->end();label->setPixmap(*pixmap);
label->resize(QSize(WIDTH,HEIGHT));
...
@How you derive or calculate WIDTH and HEIGHT is up to you.
Hope this helps ;o)
-
Thanks for the reply! Actually, when I set the background-image in stylesheet, it repeats the image automatically according to width and height. To combine the images, I actually have to write a loop for repeating the image and width and height varies lot of times. I'm thinking if there's simple way around.
-
Tiling the 'checkered' image isn't exactly complicated and its exactly what QLabel will be doing for you:
@
QLabel *label=new QLabel(this);QPixmap *tile=new QPixmap(":/checkered_tile.jpg"); QPixmap *pixmap=new QPixmap(WIDTH, HEIGHT); pixmap->fill(Qt::transparent); QPainter *painter=new QPainter(pixmap); for(int y=0; y<=HEIGHT; y+=TILE){ for(int x=0; x<=WIDTH; x+=TILE) painter->drawPixmap(x, y, TILE, TILE, *tile); } painter->drawPixmap(0, 0, WIDTH, HEIGHT, QPixmap(":/my_image.png")); painter->end(); label->setPixmap(*pixmap); label->resize(QSize(WIDTH,HEIGHT));
@
N.B. TILE is the both the width and height of my tile image.
-
[quote author="Gowtam" date="1388666787"]
I have to show the semi transparent image on the checkered background. The qlabel size keeps varying according to the size of image.
@
imageLabel->setStyleSheet("background-image:url(:/checkered.png)");
imageLabel->setPixmap(image_pixmap);@[/quote]
your code works for me in Qt4 and Qt5.Please make sure:
that the path to the checker image file is correct
that the image file is contained in the qrc file
that you rerun qmake and rebuilt your application
-
[quote author="raven-worx" date="1388670433"]
...
your code works for me in Qt4 and Qt5.
...
[/quote]Thats embarrassing, guess I should have checked that first too rather than going on what I suspect to be the case. We live and learn I guess ;o) Thanks raven-worx.
-
Just for clarification: the code of yours can be pretty simplified:
@
QPixmap pix( imagePix.size() );
QPainter p(&pix);
p.fillRect( QRect(QPoint(0,0),imagePix.size()), QPixmap(":/checker.png") );
p.drawPixmap( imagePix );
p.end();
//set
@
But this is pretty much what the stylesheet code does anyway... just with a polished QPalette. -
Thanks all! The actual problem was with image pixmap i created. While using painter, I filled with Qt::white, instead of Qt::transparent. Works fine now :)