Results 1 to 10 of 10

Thread: child label alignment problem

  1. #1
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default child label alignment problem

    Hello!
    I have two labels, one is a child of another and both are the SAME SIZE:
    Qt Code:
    1. pixmapLabel = new QLabel;
    2. greenLabel = new QLabel(pixmapLabel);
    3. myLayout->addWidget(pixmapLabel);
    4. //I don't add greenLabel (the child) to any layout
    To copy to clipboard, switch view to plain text mode 
    I want to display one over the other, so I do:
    Qt Code:
    1. pixmapLabel->setPixmap(picture);
    2. greenLabel->setPixmap(green_picture);
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: child label alignment problem

    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.

  3. The following user says thank you to wysota for this useful post:

    ht1 (21st November 2007)

  4. #3
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default problem solved

    Thanks, Wysota, for your suggestion. That did it!

  5. #4
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default a minor improvement would be nice

    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.

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: child label alignment problem

    Notice that greenLabel's position is relative to its parent, pixmapLabel.
    J-P Nurmi

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: a minor improvement would be nice

    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?

  8. #7
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: child label alignment problem

    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!

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: child label alignment problem

    Quote Originally Posted by ht1 View Post
    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.

  10. The following user says thank you to wysota for this useful post:

    ht1 (21st November 2007)

  11. #9
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default great idea

    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?

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: great idea

    Sure you can.
    Qt Code:
    1. QPixmap p1("px1.png");
    2. QPixmap p2("px2.png");
    3. QPixmap composite(p1.size());
    4. QPainter p(&composite);
    5. p.drawPixmap(p1);
    6. p.drawPixmap(p2);
    7. p.end();
    8. QLabel *l = new QLabel(this);
    9. l->setPixmap(composite);
    To copy to clipboard, switch view to plain text mode 

  13. The following user says thank you to wysota for this useful post:

    ht1 (21st November 2007)

Similar Threads

  1. problem with alignment
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2006, 12:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.