QWidget::setMinimumSize
QWidget::setMinimumSize
tommy (11th December 2007)
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)?
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.
tommy (11th December 2007)
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:
pixmapLabel->autoFillBackground();To copy to clipboard, switch view to plain text mode
Nowadays the easiest way is to use style sheets:
Qt Code:
pixmapLabel->setStyleSheet("background-color: red"); // or pixmapLabel->setStyleSheet("background-color: palette(dark)");To copy to clipboard, switch view to plain text mode
J-P Nurmi
tommy (11th December 2007)
Thank you JPN!
The style-sheets work fine to make the background a certain color. However, after doing
this (in the constructor):
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?Qt Code:
pixmapLabel->setStyleSheet("background-color: black");To copy to clipboard, switch view to plain text mode
No, it should work. Double check your code.
Just to verify it works:
Qt Code:
// main.cpp #include <QtGui> int main(int argc, char *argv[]) { QLabel label; label.setStyleSheet("background-color: black"); label.setAlignment(Qt::AlignCenter); label.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
J-P Nurmi
tommy (11th December 2007)
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:
and I try to specify the background color in the constructor of my class in the same file as my main() function:
Qt Code:
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.
I did some more digging and got my problem solved this way:
Qt Code:
pixmapLabel->setAutoFillBackground(true); 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?
tommy (11th December 2007)
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?
No, you just need to make sure there is a widget in that area that is to become blue.
People don't usually have such demands, but you can use QWidget.What widgets do people use in that case?
QPalette::Window colour role (see QPalette::ColorRole).The window itself is gray, where does that color come from?
tommy (11th December 2007)
Bookmarks