PDA

View Full Version : How do I get QPainter on top of QLabel in my QWidget



anr78
5th February 2011, 11:34
I have subclassed QWidget, created and instance of the class and placed 6 QLabels on it. In paintEvent() I use QPainter to do some painting. The result is that all the QLabels are shown on top of the QPainter-stuff. I have tried to manually call update() on my QLabels in paintEvent to make them update before I draw, but that was apparently not the right approach.

How do I control wether my QLabels or my QPaintings are shown on top?

aamer4yu
5th February 2011, 11:39
Can you show ur paintevent ?
may be you need to call the base class paintevent first.. and then do your drawing..:rolleyes:

anr78
5th February 2011, 11:47
At another computer right now, so I don't have the src. Basically I've implemented a paintEvent that just creates a QPainter and draws an image. I don't do anything about the QLabels there, so they are updated/painted outside of my control :)

The designer shows:

the_top_widget
- my_subclassed_widget
- qlabel1
- qlabel2
<and so on>

Which paintEvent did you mean? I see the smiley, but don't know enough Qt to know why you smiled, so I'll take the chance it wasn't a joke :)

aamer4yu
5th February 2011, 12:11
Dont go by the smiley.. it was just a rolleyes.. wondering.

I meant in your subclassed widget paintEvent, are you calling your base class paint event too ?

anr78
5th February 2011, 16:24
Nope. I have implemented my_subclassed_widget::paintEvent, which only does a bit of QPainting.

aamer4yu
5th February 2011, 17:48
Try calling base class event before ur drawing...
if that doesnt work, try calling raise on ur subclassed widget..

nish
5th February 2011, 18:27
if they are just qlables, they must just be showing texts or pics (hopefully). And you have them as child to your Widget which is being painted on. Basically this is not a good approach as you can just paint the qlabel stuff (pic or text) directly onto the widget. That would be better solution. Did i understood your problem correctly?

anr78
6th February 2011, 09:29
You understand correctly. The QLabels show images. Why is having them as children to my widget not a good approach?
Do you mean using QPainter::drawImage instead of QLabels? I did that before, but it seemed to cost more cycles.

nish
6th February 2011, 17:33
Why is having them as children to my widget not a good approach?
short answer, because you are having problems with this solution.
long answer, you widget is made heavy with child widgets and layout, without any benefit.


Do you mean using QPainter::drawImage instead of QLabels? I did that before, but it seemed to cost more cycles.
define "it seemed"!!.
QLabel also uses painter->draw() functions to draw its own images. The total cost of your widget would be more coz it requires more paintevent calls ( widget + labels)
Go ahead and draw everything in your paintevent.

anr78
6th February 2011, 17:47
Which I did before reading your reply, and it worked great. Thanks for the tutoring :D

wysota
6th February 2011, 21:17
The QLabels show images. Why is having them as children to my widget not a good approach?
Let's try to provide a different answer. Children of a widget are always on top of the widget and since widgets are painted using the "painter's algorithm" the child widgets are always painted after their parent. Hence you can't do anything to make them paint before the parent using the regular event processing flow.

By the way, instead of QPainter::drawImage() use QPainter::drawPixmap() storing your images as pixmaps (QPixmap) and not images (QImage), you'll see the performance is better.