1
votes

I currently draw a QImage to a QWidget with the following code:

QPainter c(this);
const QRect& cw = c.window();

c.setRenderHint(QPainter::SmoothPixmapTransform, cw.width() < scale);
c.drawImage(cw, image);

The issue is now that the image has been properly scaled to fit in the QWidget, I would like to get the pixel coordinates on the QImage based on the QWidget coordinates. Without scaling this would be fairly trivial, as widget pixels correspond directly to QImage coordinates. Ultimately, a way to translate which pixel on the QWidget corresponds to the pixel in the QImage when scaled is ideal. Any advice is much appreciated!

1
You will never be able to recover the exact pixel since when you scale the pixels are multiplied or divided, that is, a pixel can be duplicated or eliminated. What you can get a pixel with the rgb values very similar. - eyllanesc
what is scale? - eyllanesc
scale is just used to check if the actual width is less than the widget width. So in other words it is just the width of the QImage. - MateoConLechuga
What you want is to obtain an approximate pixel in the original QImage? – - eyllanesc
@eyllanesc Yes, I just want to know which one corresponds roughly to the QImage. I will probably only be using 1x, 2x, 3x scales to avoid issues, but I'm wondering if there is a better solution than the obvious one. - MateoConLechuga

1 Answers

1
votes

What you have implemented is an escalation, to recover a coordinate we can use QTransform as I show below:

QTransform tr;
tr.scale(image.width()*1.0/cw.width(), image.height()*1.0/cw.height());
QPoint imagePoint = tr.map(widgetPoint);