QGraphicsView: How to get position relative to parent object?
-
Hi,
I have a QgraphicsView + Scene with one Object (QPixmap) inside.
I try to build a "ExDropper" tool and need to get the "Pixel Position" aka the position relative to the 0,0 coordinate of the placed Pixmap. What I am missing is how to honour the image resolution(like 150dpi) and the scaling of the scene.What I have here works in a 1:1 scaling with an 100dpi image.
void QEnhancedGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
// Global coordinate-system, but widget coord. position this should fix it
//QString text;
//text = QString("%1 X %2").arg(event->pos().x()).arg(event->pos().y());
//QToolTip::showText(this->mapToGlobal(QPoint(event->pos().x(), event->pos().y())), text);emit signalSendCoordinates(this->mapToParent(event->pos()));
}thx for your hints and help
-
@ademmler said in QGraphicsView: How to get position relative to parent object?:
What I am missing is how to honour the image resolution(like 150dpi) and the scaling of the scene.
I admit I have not tested, but I don't understand this. You say you want "position relative to parent object" and that is what
mapToParent()
should give you regardless of scaling/resolution?If you do not get someone who answers your question I suggest you provide a complete minimal example which includes the object(s) and the scaling or whatever, show the output from
mapToParent()
and state what different value you want/expect it to have. -
If I understand you right - what I use should deliver the correct result?
What I am trying is to map the mouse position on screen to the pixel inside the image file.Example: The image might be RGB and has 150dpi resolution.
I load this a QPixmap into the scene at position 0,0.
When I hover over the image - I get event-pos().The image might be moved or scaled on screen.
Still I am looking for the correct position/information under the mouse.This position will be used to grab a 2x2 or 4x4 "averaged" color values from the given file.
-
@ademmler said in QGraphicsView: How to get position relative to parent object?:
What I am trying is to map the mouse position on screen to the pixel inside the image file.
Example: The image might be RGB and has 150dpi resolution.
I load this a QPixmap into the scene at position 0,0.
When I hover over the image - I get event-pos().An image pixel (pos) is a (logical) image pixel (mind blowing right? :D ) no matter if it actually equals one pixel or a block of four on your physical screen.
Check what I did here.
Is it somewhere near what you expect?
(The whole topic there might be interesting for you) -
I see - very nice approach. Unfortunately I made a mistake in my wording.
I have a CMYK Image - on hardisk, which I load into the QGraphicscene as RGB Image.
By hovering the image I want to get the coordinate, wich I use internally to read this pixel from the file!
My mapping should be screen coordinate over image to pixel coordinate in file. -
@ademmler said in QGraphicsView: How to get position relative to parent object?:
By hovering the image I want to get the coordinate, wich I use internally to read this pixel from the file!
Either I don't get what you are asking for or you don't understand the solution from the topic I've linked above.
How should we or Qt know what you do "internally"?
And only a graphical representation of some image/pixel data has "DPI". Your image on your hard drive does not obviously.
If you resize the image or change the aspect ratio or something else at runtime, you have to keep that in mind...manually.
There is no direct Qt "mapping" or function to map any widget (view) mouse position to a certain pixel from some file on your hard drive.QImage
for example has functions likeor
which might be helpful for manual translation.
-
Hi @Pl45m4
Hi, thank you for your ideas and hints.
From your approach I have understood that you the the pixelValues from the image wich is placed as aQLabel. In this case the simple event->pos maps to the pixel from the image. Right?
In my approach the image is maybe in a different resolution than the RGB representation on screen.
Hence I was asking about ideas how to map between the two.I ll check the links you gave me if they are helpful.
-
@ademmler said in QGraphicsView: How to get position relative to parent object?:
From your approach I have understood that you the the pixelValues from the image wich is placed as aQLabel. In this case the simple event->pos maps to the pixel from the image. Right?
My point was, that no matter how your image is displayed or even printed via some print device (
QPrinter
) on paper, code likeQPixmap pixmapImage("filename.png"); pixmapImage.toImage().pixel(X, Y);
will always address the "real" pixel ( X / Y ).
The only thing you have to do is the translation of the view coordinates, sincemapTo
ormapFrom
does not respect any image transformation you do manually...it's just the plain "parent-child" widget coordinate system mapping.