Results 1 to 17 of 17

Thread: empty pixmap as a QLabel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default empty pixmap as a QLabel

    Hi,

    My program removes a pixmap (a QLabel) from the screen and replaces it back as the user clicks a button.
    Everything works fine except for the fact that when the pixmap is removed from the QLabel, other graphics collapses onto where the pixmap originally was. I need something to hold the pixmap's place until it is put back. This means I need to put something invisible in the pixmaps place while it's gone. I have tried a transparent pixmap but for some reason this lets the graphics to collapse, too. The program doesn't think the transparent pixmap has any "substance". It treats the transparent pixmap like air by letting other widgets take it's place.
    Question: What do you use as an invisible placeholder for a pipxmap that's displayed as a QLabel? A QRect? Something else?

  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: empty pixmap as a QLabel

    Subclass QLabel and either reimplement its sizeHint() so that when you remove the pixmap its size is still returned by the label or reimplement paintEvent() so that when you set some flag, the pixmap is not drawn (but don't remove it so that a proper size hint is returned).

    Or set a minimum size on the label so that even when it is empty, it won't collapse to a smaller size.

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

    tommy (10th December 2007)

  4. #3
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: empty pixmap as a QLabel

    Thank you!
    How do you set minimum QLabel size?

  5. #4
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: empty pixmap as a QLabel

    QWidget::setMinimumSize

  6. The following user says thank you to pherthyl for this useful post:

    tommy (11th December 2007)

  7. #5
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: empty pixmap as a QLabel

    Thank you, that worked.

    My other question is:
    Is it possible to give the QLabel a default color; so that when it does not display a pixmap it shows a default background color (instead of being invisible)?

  8. #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: empty pixmap as a QLabel

    Yes. set its window palette role to the colour you want and make sure autoFillBackground property is set to true.

    Note - if you continue to expand requirements on the label, it might prove much simpler to write a custom widget that does all you want - it's just a few lines of code.

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

    tommy (11th December 2007)

  10. #7
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: empty pixmap as a QLabel

    Thanks but I still don't know hot to actually specify the background color of the QLabel. The below code is not doing it:

    Qt Code:
    1. pixmapLabel->setBackgroundRole(QPalette::Dark);
    2. pixmapLabel->autoFillBackground();
    To copy to clipboard, switch view to plain text mode 

  11. #8
    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: empty pixmap as a QLabel

    Nowadays the easiest way is to use style sheets:
    Qt Code:
    1. pixmapLabel->setStyleSheet("background-color: red");
    2. // or
    3. pixmapLabel->setStyleSheet("background-color: palette(dark)");
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  12. The following user says thank you to jpn for this useful post:

    tommy (11th December 2007)

  13. #9
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: empty pixmap as a QLabel

    Thank you JPN!

    The style-sheets work fine to make the background a certain color. However, after doing
    this (in the constructor):
    Qt Code:
    1. pixmapLabel->setStyleSheet("background-color: black");
    To copy to clipboard, switch view to plain text mode 
    everything is always black and the actual pixmap that the QLabel (pixmapLabel) is supposed to show is all black and the picture is not seen.Looks like the background is sitting on top of the pixmap, not the other way around. Seems like I need to specify something more?

  14. #10
    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: empty pixmap as a QLabel

    Quote Originally Posted by tommy View Post
    everything is always black and the actual pixmap that the QLabel (pixmapLabel) is supposed to show is all black and the picture is not seen.Looks like the background is sitting on top of the pixmap, not the other way around. Seems like I need to specify something more?
    No, it should work. Double check your code.

    Just to verify it works:
    Qt Code:
    1. // main.cpp
    2. #include <QtGui>
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6. QLabel label;
    7. label.setPixmap(QPixmap(":/trolltech/styles/commonstyle/images/standardbutton-help-128.png"));
    8. label.setStyleSheet("background-color: black");
    9. label.setAlignment(Qt::AlignCenter);
    10. label.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  15. The following user says thank you to jpn for this useful post:

    tommy (11th December 2007)

  16. #11
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: empty pixmap as a QLabel

    JPN,

    Indeed, your suggestion is great and it works but for some reason it is not working for me. My QLabel is declared in the *.h file using pointers:

    Qt Code:
    1. QLabel* pixmapLabel;
    To copy to clipboard, switch view to plain text mode 

    and I try to specify the background color in the constructor of my class in the same file as my main() function:

    Qt Code:
    1. pixmapLabel = new QLabel;
    2. pixmapLabel->setStyleSheet("background-color: black");
    To copy to clipboard, switch view to plain text mode 

    These are the only differences from what you are suggesting. I wonder if these could make a difference? Unfortunately I have to use the pointer apprach in my program.

Similar Threads

  1. QLabel size policy
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2007, 17:57
  2. How to paint a selection rectangle on a pixmap?
    By SkripT in forum Qt Programming
    Replies: 6
    Last Post: 8th January 2006, 19: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.