PDA

View Full Version : diplay and resize bitmap



tommy
12th November 2007, 19:15
Hi all,

Does anybody know whow to simply display an image from a bitmap file (8bpp) on a widget? Basically how to show a bitmap photo file on the screen and (if that's possible) also force it to be a certain size (compress it to smaller than original).

I'd like to the be able to do:
pixmapLabel->setPixmap(bmpImageFromFile);
mainLayout->addWidget(pixmapLabel);

Any ideas are a huge help! Thanks.

wysota
12th November 2007, 19:26
Your code above is correct. You should be able to do that this way.

int main(int argc, char **argv){
QApplication app(argc, argv);
QLabel lab;
lab->setPixmap(QPixmap("somefilepath"));
lab.show();
return app.exec();
}

high_flyer
12th November 2007, 19:29
you are not far from mark:
use QPixmap (http://doc.trolltech.com/4.3/qpixmap.html#QPixmap-3) to load the image and QLabel::setPixmap() (http://doc.trolltech.com/4.3/qlabel.html#pixmap-prop)
You can then add the QLabel to a layout.

EDIT: I was too slow :)

tommy
13th November 2007, 17:06
Thank you very much for your help!

I was wondering how you resize the loaded image from bitmap file?
Is the best way to resize the image by forcing it fit onto a widget of a smaller size? Somehow.

Also, once you have diplayed your bitmap, is it possible to display something over it (for example text) so that the bitmap underneath is still seen? I guess this is a question about layers.

wysota
14th November 2007, 01:34
QPixmap::scaled() or QImage::scaled() are your friends. You can paint over an image as well, if you want, without any layers. If you want layers, you can position one widget over another. Or use QGraphicsView. It really depends what you want to obtain.

tommy
14th November 2007, 15:56
Thanks wysota!

Since you so kindly offered your help, I'd like to ask you how you position one widget over another so that both show? Layers would be a good solution. I only know how to show one widget in any given position?
My goal is to display a bitmap image (QPixmap) and show it. Then draw a circle over it using another layer (display one widget over another, as you suggested). I'd then like to remove the upper layer (the circle) when user checks the check-button and display the layer with the circle again when the user unchecks the check button. Kind of a toggle view.
My first obstacle is to find a way to display two widgets one over the other so that both show.
Thanks again.

wysota
14th November 2007, 17:53
I'd like to ask you how you position one widget over another so that both show?
Make one widget child of another.


display one widget over another, as you suggested
I didn't suggest anything like that. Your goal seems perfect for QGraphicsView or for subclassing QLabel and reimplementing its paintEvent() to draw the additional circle depending on the state of a property.

tommy
14th November 2007, 18:28
Wysota,

Sorry for another (most likely) stupid question but how do you make one widget a child of another?
And once you have that achieved, how do you then set them. So far I've been doing this when I display something:

myLabel->setPixmap(pm);

But I can't do the same thing twice for the same label?

wysota
14th November 2007, 18:35
Sorry for another (most likely) stupid question but how do you make one widget a child of another?
You pass a pointer of the parent widget to the constructor of the child widget.


But I can't do the same thing twice for the same label?
You can't, but you can have two labels. But again, based on what you said here, it's not a good approach for you.