PDA

View Full Version : How to show single image as background in label



nrabara
4th December 2009, 05:30
Hi,

I want to show single image in background of QLabel. If the label size is big(say 640 by 480) and if image size is small (say 64 by 64). then background will show so many no of images.

But I want to show only single image with original size in center.

Can anybody suggest me right way to do this.

I am trying following...




QLabel label;
QPalette palette;

label.setGeometry(0,0,640,480);
QImage img("/home/nirav/img.png");

palette.setBrush(label.backgroundRole(), QBrush(img));

label.setPalette(palette);

label.show();



Nirav
"Your suggestion would be a great help for me"

Lykurg
4th December 2009, 06:24
QLabel::setPixmap().

yogeshgokul
4th December 2009, 06:26
Hi,

I want to show single image in background of QLabel. If the label size is big(say 640 by 480) and if image size is small (say 64 by 64). then background will show so many no of images.

But I want to show only single image with original size in center.

Can anybody suggest me right way to do this.

I am trying following...




QLabel label;
QPalette palette;

label.setGeometry(0,0,640,480);
QImage img("/home/nirav/img.png");

palette.setBrush(label.backgroundRole(), QBrush(img));

label.setPalette(palette);

label.show();



Nirav
"Your suggestion would be a great help for me"
Pls try this.
1. You need to use a frame.
2. Put QLabel in the frame.
3. Set your image as QPixmap to that label.
4. Set the Alignment Center (HCenter | VCenter)
5. Set Pixmap scaling to content turned off.

nrabara
7th December 2009, 06:13
I have tries as per your suggestion.



QImage img("/home/nirav/img.png");

QFrame frame;
frame.setGeometry(0,0,250,250);
frame.show();

QLabel label(&frame);
label.setGeometry(0,0,200,200);

QPixmap pixmap;
pixmap = pixmap.fromImage(img);

QPalette palette;
palette.setBrush(label.backgroundRole, QBrush(pixmap));

label.setPalette(palette);
label.show();



but still I am getting some many no of images in label .

I thing this happens because when I create a object of QBrush by passing Pixmap as argument in constructor it takes Qt::TexturePattern. that's way I am getting no of image instead of single one.

This Qt:: TexturePattern is default for QBrush::QBrush(const QImage & img) & QBrush::QBrush(const QPixmap & pixmap)

Can anybody help me solve this problem & how to show a single image in background instead of so many images.

Suggestion would be appreciated.

Lykurg
7th December 2009, 10:36
but still I am getting some many no of images in label .

I thing this happens because when I create a object of QBrush by passing Pixmap as argument in constructor it takes Qt::TexturePattern. that's way I am getting no of image instead of single one.

What a surprise. Use QLabel::setPixmap()!

nrabara
16th December 2009, 11:33
Yes, QLabel::setPixmap() .

Thanks .