PDA

View Full Version : How to paint a selection rectangle on a pixmap?



SkripT
8th January 2006, 19:41
Hi, I'm using Qt4 opensource version on Windows XP. I want to create a simple photo editor program. One of the options is to extract a rectangular part of the image and save it into a new image. The original image can be very large so my idea is to insert the image as a QPixmap in a QLabel and this QLabel is inserted in a QScrollArea. I have subclassed QScrollArea to reimplement the paintEvent method. This is the constructor:

FotoAreaEditor::FotoAreaEditor(EditorFotos *parent)
: QScrollArea(parent)
{
fotoLabel = new QLabel;
fotoLabel -> setScaledContents(true);
fotoLabel -> setPixmap(QPixmap("../book.png"));

setWidget(fotoLabel);
setWidgetResizable(false);
setBackgroundRole(QPalette::Dark);
}

In the paintEvent method I create a QPainter object with parent the viewport of the QScrollArea and I try to draw a rectangle on the label just to view the results:

void FotoAreaEditor::paintEvent(QPaintEvent *event)
{
//QScrollArea::paintEvent(event);

QPainter painter(this -> viewport());
painter.setPen(Qt::blue);
painter.setBrush(Qt::Dense1Pattern);
painter.drawRect(10, 20, 80, 60);
}

but only the pixmap in the QLabel is painted. And if I don't set any pixmap in the qlabel the rectangle is painted. Does anybody know why?. What am I doing wrong? I dont' have much experience with QPainter...

Thanks.

axeljaeger
8th January 2006, 19:46
You should not use a QLabel but rather also paint the visible part of your image in the paintEvent of your scrollarea. The problem is that you paint in the scrollview but the label is above everything you paint.

SkripT
8th January 2006, 20:11
Ok, I understand. But this solution will cause a lot of flickering when I move the scroll bars. It' wouldn't be more simple just inserting the image as the background of the QScrollArea with setPalette?

axeljaeger
8th January 2006, 20:20
It will not cause a lot of flickering if done right, e.g. using double buffering

SkripT
8th January 2006, 20:29
Yes you are right. But I will be very grateful if you can give me some more details of how to do this double buffering (I haven't much practice working with images...)

axeljaeger
8th January 2006, 20:48
If you are using Qt4, you're fine: Just draw the pixmap using QPainter::drawPixmap. If you use Qt3, first draw in a QPixmap and then bitBlt the QPixmap to the screen.

I think Johan's pixmap view provides source that could be helpfull:

http://www.digitalfanatics.org/e8johan/projects/jseries/index.html

SkripT
8th January 2006, 20:52
Ok thanks again