PDA

View Full Version : QImage Help Pls



munna
5th June 2006, 10:30
Hi,

I have a widget of size, lets say (400,400). This widget i used to show an image. Also, this widget has a kind of box or frame of size (350,350). When user opens an image file, if the image size is bigger than the frame, then the part of the image that is outside the frame should look a little hazy whereas the part of the image that is inside the frame should be clear.

How can I do it?

Thanks a lot.

jpn
5th June 2006, 11:15
You could use QLabel for showing the image, then use custom painting to draw the frame and for example a transparent gray layer over the part which should be "hazy".



void ImageLabel::paintEvent(QPaintEvent* e)
{
// let the label paint the image (setPixmap()) underneath
QLabel::paintEvent(e);

// init painter for custom drawing
QPainter painter(this);

// draw a frame over the image (black, 2 pixels wide)
QRect frameRect(0, 0, 350, 350);
painter.setPen(QPen(Qt::black, 2));
painter.setBrush(Qt::NoBrush);
painter.drawRect(frameRect);

// calc clip region
QRegion clipRegion(rect());
clipRegion = clipRegion.subtract(frameRect);
painter.setClipRegion(clipRegion);

// draw a gray layour over the rest to make it look hazy
painter.setPen(Qt::NoPen);
QColor hazyColor(QColor(Qt::gray));
hazyColor.setAlpha(100);
painter.setBrush(QBrush(hazyColor));
painter.drawRect(rect());
}


PS. above code is written on the fly and is not guaranteed to work 100% ;)

munna
5th June 2006, 11:39
Thanks a lot for the code. Is kind of working but not perfectly. I will look into it, and post back the working code.

Thanks.

munna
5th June 2006, 11:48
Another related help please.

How can I draw the image centered ?

What i mean is, if the image size is, lets say 500,500, then for the widget of size 400,400, the image should be drawn from -50 to 450 (x-scale) and -50 to 450 (y-scale), so that the center of the image co-incides with the center of the widget.

How can I do this ?

Thanks a lot.

jpn
5th June 2006, 12:02
I'm not sure if QLabel is capable of that, you might have to draw the pixmap yourself then.
At least you can make use of:
void QStyle::drawItemPixmap ( QPainter * painter, const QRect & rect, int alignment, const QPixmap & pixmap ) const [virtual] (http://doc.trolltech.com/4.1/qstyle.html#drawItemPixmap)