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
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.

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

    tommy (10th December 2007)

  3. #2
    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?

  4. #3
    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

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

    tommy (11th December 2007)

  6. #4
    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)?

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.

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

    tommy (11th December 2007)

  9. #6
    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 

  10. #7
    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

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

    tommy (11th December 2007)

  12. #8
    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?

  13. #9
    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

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

    tommy (11th December 2007)

  15. #10
    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.

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

    Default got something else to work

    I did some more digging and got my problem solved this way:

    Qt Code:
    1. QPalette Pal2(pixmapLabel->palette());
    2. Pal2.setColor(QPalette::Window, Qt::black);
    3. pixmapLabel->setAutoFillBackground(true);
    4. pixmapLabel->setPalette(Pal2);
    To copy to clipboard, switch view to plain text mode 

    This code sets the background of my QLabel (pixmapLabel) to black as I wanted it. But this code doesn't work on layouts. I guess this is because layouts don't paint. However, sometimes you need to change the color of a layout even if it doesn't paint. How do you do that. For example: My layout consists of 2 sub-layouts (each with its own widgets etc). I want the top one to be red and bottom to be green. How do you do it?

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

    Default Re: got something else to work

    Quote Originally Posted by tommy View Post
    But this code doesn't work on layouts.
    Hmm?? There is no such thing as a color of a layout. Layouts don't have colours - it's like you wanted to change the colour of a tcp socket... If you want to change the colour of some area, you have to have a widget there.

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

    tommy (11th December 2007)

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
  •  
Qt is a trademark of The Qt Company.