PDA

View Full Version : [SOLVED] Display order (z-axis) of objects in a same widget



Mystogan
9th March 2016, 09:22
Hey Folks,

I am new to Qt and i got a problem on an application I am trying to code.
First of all, i am using Qt5 on Ubuntu.
That being said, I am trying to code a widget containing a QImage (png) and a label. I would like that at some moment, the image goes on top of the label. A hide() method isn't what I am looking for as my image is not plain and do not recover entirely my label, I still want it to be "part" visible.

My problem is that my label is always on top and never in background of my QImage, and I don't know how to manage it from my repaint event.

Here is what my code looks like:


QImage myPicture = QImage("./Foo.png");
QPainter painter(this);
QPainterPath path;

path.addText(0, 0, painter.font(), "Foo");
painter.drawPath(path);
painter.drawImage(myPicture.rect(), myPicture, myPicture.rect());


Does anybody have an idea on this problem of mine ?

Thanks for having a look.

anda_skoa
9th March 2016, 09:55
If you don't use a label but draw the text yourself, you have full control over when you draw which part.

Alternatively you can use a QGraphicsView and use two sibling items for the image and text and change their Z value accordingly.

Cheers,
_

Mystogan
9th March 2016, 10:22
If you don't use a label but draw the text yourself, you have full control over when you draw which part. _

Can you explain it a bit deeper please ?

The way my code works, the text is always on top of the QImage, how can I control on the z-level of each element ?

The QGraphicsView seems to have all the methods to control this z-level, but I was wondering if it was possible to manage it easily the way I coded it initially....

anda_skoa
9th March 2016, 11:19
Can you explain it a bit deeper please ?

Text under image


painter.drawText(...);
painter.drawImage(...);

Text over image


painter.drawImage(...);
painter.drawText(...);


Cheers,
_

Mystogan
9th March 2016, 13:16
Nice, it works well with the drawText method !! Thank you !

However do you know why it wouldn't work using the path.addText/drawPath methods ?

Thanks again for your help.

anda_skoa
9th March 2016, 13:28
You mean using drawPath() after drawImage()? That should work as well.

Chrers,
_