PDA

View Full Version : Need Graphical Text on QPushButton/QLabel



JimDaniel
3rd June 2008, 03:00
I'm wanting to create more graphical text to put on our QPushButtons and QLabels. I mean text with effects, like gradients...outlines...etc.

How would you gurus recommend approaching something like this? I'd like to avoid pre-creating images for all the text I'll need, as we'll need to translate it later on. I know QPainter can achieve all the effects I need.

Would the best method be to get the region of the button or label I want the graphical text on, then use QPainter to paint the text I need inside the area? That was my initial thought, but somehow I don't like that.

Thanks for any help you can give, Daniel

EDIT: Been reading up on QPainter - I notice it can take a QPaintDevice in its constructor as the object to paint to. Looking at QPaintDevice I see that QPixmap implements it. Would it be feasible (efficient) to paint my graphical text to a QPixmap object, then set the QPixmap as the button's icon? I'm going to experiment of course, just want to do it the best possible way.

aamer4yu
3rd June 2008, 04:57
Use stylesheets :)
You can then play with qlineargradient or other. Refer Qt Style sheets in Assistant

wysota
3rd June 2008, 06:47
I'm wanting to create more graphical text to put on our QPushButtons and QLabels. I mean text with effects, like gradients...outlines...etc.

I think you'll have to subclass the button (or the label) and implement the functionality you want. You can use pixmaps as you suggested, but the result will probably be not what you want.

JimDaniel
7th June 2008, 03:02
I think you'll have to subclass the button (or the label) and implement the functionality you want. You can use pixmaps as you suggested, but the result will probably be not what you want.

Not only was it not what I wanted, but I couldn't get it to work that way at all.

I ended up subclassing, and drawing my own text. I'm having more difficulty though.

See, I want the button to be drawn as it normally is, so first I pass the paintEvent up to the parent QPushButton paintEvent. Then I draw my own graphical text. The problem is, I want to be able to set the text and icon the normal way, but if I do that, then the parent paintEvent draws text/icon first. What is the best way to stop the parent from drawing text/icon, and still keep the normal way of setting those properties? Does that make sense? I guess I can think of a work-around for it, like storing a temp variable somewhere, but I'm wondering if there is a cleaner way.

wysota
7th June 2008, 10:32
In general you can use QStylePainter that will do most of the work for you. You'll be passing a QStyleOption subclass to it and there you can control the text and pixmap that will be drawn. You can pass empty values there so that the style will draw everything apart the text and icon. Then it's just a matter of calculating the shift for pressed buttons, colours for disabled buttons and then you can draw your effects.

JimDaniel
7th June 2008, 16:39
Great, that's just what I was looking for. Is there also a way to get at the positioning info for the text and icon? Or is that something I'm just going to have to calculate myself?

wysota
9th June 2008, 08:12
Calculate it yourself or check what QStyle has to offer.