Results 1 to 17 of 17

Thread: empty pixmap as a QLabel

  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.

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

  18. #13
    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: 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.

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

    tommy (11th December 2007)

  20. #14
    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

    Wysota,

    So you say that if I want to have a large blue window with a small red button in the middle of it, it's going to be a difficult thing to do? I have to cover the empty area with some widgets to give it some color? I am really confused. What widgets do people use in that case? The window itself is gray, where does that color come from?

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

    Quote Originally Posted by tommy View Post
    So you say that if I want to have a large blue window with a small red button in the middle of it, it's going to be a difficult thing to do?
    No, you just need to make sure there is a widget in that area that is to become blue.

    What widgets do people use in that case?
    People don't usually have such demands, but you can use QWidget.

    The window itself is gray, where does that color come from?
    QPalette::Window colour role (see QPalette::ColorRole).

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

    tommy (11th December 2007)

  23. #16
    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

    So just an imaginary widget will do? Like for example a QRect that has the dimentions of the window and is sitting underneath the other widgets somehow?

  24. #17
    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

    Not imaginary. A real QWidget with its own layout, etc. A widget - QRect is not a widget.

    If you have a widget that contains a layout that contains another layout (for example a horizontal box layout containing a vertical box layout) you need to put a widget between those two layouts - set the vertical one as the new widget's layout and add the widget to the horizontal layout. Then you'll be able to change the colour of the area covered by that new widget.

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.