PDA

View Full Version : empty pixmap as a QLabel



tommy
10th December 2007, 20:50
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?

wysota
10th December 2007, 21:40
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.

tommy
10th December 2007, 23:21
Thank you!
How do you set minimum QLabel size?

pherthyl
10th December 2007, 23:43
QWidget::setMinimumSize

tommy
11th December 2007, 00:19
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)?

wysota
11th December 2007, 00:39
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, 16:36
Thanks but I still don't know hot to actually specify the background color of the QLabel. The below code is not doing it:



pixmapLabel->setBackgroundRole(QPalette::Dark);
pixmapLabel->autoFillBackground();

jpn
11th December 2007, 16:40
Nowadays the easiest way is to use style sheets:


pixmapLabel->setStyleSheet("background-color: red");
// or
pixmapLabel->setStyleSheet("background-color: palette(dark)");

tommy
11th December 2007, 16:52
Thank you JPN!

The style-sheets work fine to make the background a certain color. However, after doing
this (in the constructor):


pixmapLabel->setStyleSheet("background-color: black");

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?

jpn
11th December 2007, 17:02
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:


// main.cpp
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel label;
label.setPixmap(QPixmap(":/trolltech/styles/commonstyle/images/standardbutton-help-128.png"));
label.setStyleSheet("background-color: black");
label.setAlignment(Qt::AlignCenter);
label.show();
return app.exec();
}

tommy
11th December 2007, 17:30
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:



QLabel* pixmapLabel;


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



pixmapLabel = new QLabel;
pixmapLabel->setStyleSheet("background-color: black");


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.

tommy
11th December 2007, 18:47
I did some more digging and got my problem solved this way:



QPalette Pal2(pixmapLabel->palette());
Pal2.setColor(QPalette::Window, Qt::black);
pixmapLabel->setAutoFillBackground(true);
pixmapLabel->setPalette(Pal2);


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?

wysota
11th December 2007, 19:51
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.

tommy
11th December 2007, 20:24
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?

wysota
11th December 2007, 20:38
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).

tommy
11th December 2007, 21:12
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?

wysota
11th December 2007, 21:15
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.