PDA

View Full Version : Inaccurate MouseEvent position over a Scaled Pixmap



ramstormrage
17th May 2008, 13:04
Hello experts,

Ive got this label with a pixmap on it which is scaled to fit. Now, I have subclassed this label to return the color of the pixel under the mouse cursor whenever I click on it. The problem is I dont get accurate pixel colors. However, I tried an image that is not scaled and it works perfectly.

Is there a way to get the correct position in scaled-to-fit mode? Thank you..

jpn
17th May 2008, 16:14
May we see how you do it? Of course you have to take the scaling into account. Maybe it would be easier to write your own simple "PixmapWidget" to get direct access to the scaled pixmap.

ramstormrage
18th May 2008, 04:13
Hi jpn,

Thank you for answering..
Ive attached the folder of my sample program.. It has the codes for the custom frame, custom label and the dialog.. Sorry if I cant make it any simpler, its as simple as it gets..

Whenever you click inside the label, the frame on the lower right corner changes color depending on the pixel selected. If you'd try clicking around, you'll see that it doesnt work like it should..

Id like to know more about the "simple PixmapWidget" you were suggesting earlier. How is it done?

Thank you in advance

jpn
18th May 2008, 10:28
Whenever you click inside the label, the frame on the lower right corner changes color depending on the pixel selected. If you'd try clicking around, you'll see that it doesnt work like it should..
Yeah, because you get the pixel from the ORIGINAL image but the label shows a different SCALED image.


Id like to know more about the "simple PixmapWidget" you were suggesting earlier. How is it done?
A QFrame subclass with two private members, original pixmap and scaled pixmap. In resizeEvent() you scale the original pixmap according to width() and height() and store it as scaled pixmap. In paintEvent() you draw the scaled pixmap. In mousePressEvent() you get the pixel from the scaled pixmap.

ramstormrage
18th May 2008, 14:59
Its a Qframe subclass not a QLabel one?
Im not really familiar with overriding the resizeEvent() and paintEvent() handler. Could you provide me with a minimal example so I could understand it more?

Thank you..

jpn
18th May 2008, 15:31
Its a Qframe subclass not a QLabel one?
Yes, because you'll draw the pixmap yourself and therefore QLabel offers nothing you're interested in.


Im not really familiar with overriding the resizeEvent() and paintEvent() handler. Could you provide me with a minimal example so I could understand it more?
You already reimplemented some mouse event handlers for your custom label. Reimplementing paintEvent() and resizeEvent() doesn't differ syntactically. Use QPainter to draw the pixmap in paintEvent() and QPixmap::scaled() to scale the pixmap in resizeEvent().

ramstormrage
22nd May 2008, 13:51
Hello again jpn,

Sorry.. I dont understand how would there be a resizeEvent() in a QFrame. Could you give a me pseudocodes so I can understand more? Im lost..


//I call this function when the dialog is invoked or when I want to place a new Pixmap
void simplePixmapWidget::loadPixmap(QImage &img)
{
originalPx = new QPixmap(QPixmap::fromImage(img));
newPx = new QPixmap(width(), height());
}

void simplePixmapWidget::resizeEvent(QResizeEvent *ev)
{
QSize size(width(), height());
newPx->scaled(size, Qt::IgnoreAspectRatio, Qt::FastTransformation);

//calls paintEvent afterwards
}

void simplePixmapWidget::paintEvent(QPaintEvent *ev)
{
QPainter(this);
painter.drawPixmap(where?, newPx);
}

jpn
22nd May 2008, 14:22
First of all, there is no need to allocate QPixmaps on the heap. It's an implicitly shared class (http://doc.trolltech.com/4.4/shared.html):


// QPixmap* originalPx;
// QPixmap* newPx;
QPixmap originalPx;
QPixmap newPx;

originalPx = QPixmap::fromImage(img);
newPx = QPixmap(width(), height());

Notice that QPixmap::scaled() RETURNS the scaled pixmap and you might want to always scale the original pixmap not to lose information over time:


newPx = originalPx.scaled(size, Qt::IgnoreAspectRatio, Qt::FastTransformation);

Because you ignore aspect ratio and the pixmap size always matches widget size, you can just draw the pixmap in the upper left corner (0,0):


painter.drawPixmap(0, 0, newPx);

If you later decide to keep aspect ratio, then you can use this handy method in QStyle to draw the pixmap as aligned:


style()->drawItemPixmap(&painter, rect(), Qt::AlignCenter, newPx);

ramstormrage
26th May 2008, 10:42
Hi jpn,

Thank you for the clarifications.
I did some modifications and I placed the codes from the resizeEvent to loadPixmap() because I wanted to be able to change the Pixmap whenever necessary.


void simplePixmapWidget::loadPixmap(QImage &img)
{
origPx = QPixmap::fromImage(img);
newPx = QPixmap(width(), height());
QSize size(width(), height());
newPx = origPx.scaled(size, Qt::IgnoreAspectRatio, Qt::FastTransformation);
QImag0 = newPx.toImage();
update();
}

Thank you very much.

jpn
26th May 2008, 10:48
Call QWidget::update() to schedule a paint event.