PDA

View Full Version : child label alignment problem



ht1
21st November 2007, 18:51
Hello!
I have two labels, one is a child of another and both are the SAME SIZE:


pixmapLabel = new QLabel;
greenLabel = new QLabel(pixmapLabel);
myLayout->addWidget(pixmapLabel);
//I don't add greenLabel (the child) to any layout

I want to display one over the other, so I do:


pixmapLabel->setPixmap(picture);
greenLabel->setPixmap(green_picture);


1) Although both pixmaps are the exact same size, they appear slightly offset when displayed one over the other. Why?
2) When I change the size of both of the pixmaps (again they remain identical in size) and let addStretch push the parent label (pixmapLabel) to a slightly new location, the child won't move to the same location. Is this happening because the child pixmap is not attached to any layout? Is there a way to push the child widget with addStretch the same way as its parent.
Thank you much!

wysota
21st November 2007, 19:52
I don't know what you are trying to do (not a very straightforward thing, I presume), but you should probably set the geometry of the floating label. In both cases.

ht1
21st November 2007, 20:28
Thanks, Wysota, for your suggestion. That did it!

ht1
21st November 2007, 20:54
Hi,

It seems like 99% of the problems are solved. Even with geometry set and sizes exactly the same, the two pixmaps that I'm trying to align won't align perfectly. It's very close but not perfect. It seems that when a pixmap is loaded from a file as opposed to when it's drawn by my Qt program, the image is 1 pixel taller (despite the fact that they were made exactly the same with "scaled()" command). I wonder if this has something to do with pixmap margins?? Or maybe someone has some other ideas.

jpn
21st November 2007, 20:58
Notice that greenLabel's position is relative to its parent, pixmapLabel.

wysota
21st November 2007, 21:17
This might be a stupid question, but why don't you compose a single pixmap out of the two pixmaps and set it to one label, so that you can get rid of the other?

ht1
21st November 2007, 21:25
All of your comments are very relevant (thanks wysota and jpn).
I have to use two pixmaps because I use a checkbox to turn different pixmaps on/off. Everything works really well except for the fact that two pixmaps of the same size won't align perfectly. I guess I could try converting both of them to files and then reload from the file. Problem is that that would slow things down and it wouldn't be as elegant.
Another thing I'm suspecting is that the scaled() function may work a bit differently when the background is transparent (as is the case the child label here). Anyway, I keep pushing because I'm very close to what I want to achieve. If any of you have some other ideas, I'm of course all eyes and ears!

wysota
21st November 2007, 21:31
All of your comments are very relevant (thanks wysota and jpn).
I have to use two pixmaps because I use a checkbox to turn different pixmaps on/off.
That doesn't determine you have to use two labels... You can have two pixmaps and then render them to a third pixmap which you can then show using the label.

ht1
21st November 2007, 21:46
Wysota, this is a great idea!
I didn't know you could automatically put two existing bitmaps together like that. How do you do it?

wysota
21st November 2007, 21:50
Sure you can.

QPixmap p1("px1.png");
QPixmap p2("px2.png");
QPixmap composite(p1.size());
QPainter p(&composite);
p.drawPixmap(p1);
p.drawPixmap(p2);
p.end();
QLabel *l = new QLabel(this);
l->setPixmap(composite);