[Interest] QLabel with images

Konrad Rosenbaum konrad at silmor.de
Tue Sep 11 16:18:54 CEST 2012


On Tuesday 11 September 2012 15:20:33 Sensei wrote:
> I need to display a QLabel that has an image and text in it, in
> particular, a semi-transparent icon. All, without implementing another
> custom widget.
> Googling, I found no ways of adding text and images, since one excludes
> the other. Moreover, my images are black and transparent, used for some
> QIcons I need, and I'd like to avoid creating another set of images,
> just for the transparency.
> What I've done now is having two overlapping QLabels, one for the text,
> the other for the icon. Now I just need to make the icon at a certain
> alpha, but setWindowOpacity does not work on the QLabel containing the
> pixmap.
> Any hints?

A few:

Overlapping QLabels is not a good idea - you may get very weird effects next 
time anything changes in the implementation or next time you port to a new 

Create a pixmap and paint the icon and the text over it. Once you've done that 
for the first time it becomes very easy.

Some untested code:

//load the icon
QPixmap pix("/my/iconfile.png");
//enable painting
QPainter paint(&pix);
//draw the text
paint.drawText(MY_X,MY_Y,"my text");

//create the label
QLabel *mylabel=new QLabel(...);

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120911/e725ec29/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120911/e725ec29/attachment.sig>

More information about the Interest mailing list