Solved Exact correct size needed
-
So you want to display a rect only on the first view? You could create two scenes and two views and add only the rect into the first one. If the second view is some kind of "only selected view" you can take the rect position and size to crop the image and set it to the QGraphicsPixmapItem on the second scene.
-
@ollarch said in Exact correct size needed:
Hi @ollarch. Your post has just crossed with the latest UPDATE I have typed into my post above!So you want to display a rect only on the first view? You could create two scenes and two views and add only the rect into the first one.
Nooo, I don't think so! 2 scenes?! Just for a shape on a view?
I'm pretty sure now that I understand a bit more that what I am doing via
QGraphicsView::drawForeground()
is the correct, cheapest way to draw a temporary foreground shape --- which is not an object on the scene --- to follow the mouse.You can see there that I am getting so excited now that I understand layers --- background -> any graphics objects -> foreground --- plus the fact that
QGraphicsView
draws its own layers, QGraphicsView::drawForegroundReimplement this function to provide a custom foreground for this view.
The default implementation fills rect using the view's foregroundBrush. If no such brush is defined (the default), the scene's
drawForeground()
function is called instead.So I now think I ought best move the map pixmap out from being a graphics object and into the scene's background layer instead. I am enjoying this as I begin to understand :)
-
If I understand it. When the mouse enters the view you want to display a rect (or circle) centered to the mouse position?
-
@ollarch
Yes. (Only a touch more complex than that: it has to map to nearest rect/circle/shape where the map is notionally divided into a grid of distinctly-located shapes, rather than continuous, and "snap" to there. But that is a detail.) And this shape must only be drawn on the view where the mouse is being moved; it does not belong to the scene, and if there were multiple views it must not appear in views other than the one with the mouse move. That's why it has to be view-based, not scene-based.But I am already at this point now, I have achieved what I need, using
QGraphicsView::drawForeground()
:) -
Hi,
Do you have a second view, for what?
-
@ollarch
As I wrote earlier, presently I do not, but there is no reason I should not introduce one at a later date. One could have multiple views onto the same scene, so that user could see different areas of map in different windows (views) if desired.At which point I would only want the foreground shape following the user's mouse to appear in the one view where he is currently moving the mouse, not any other views (even if they happened to observe the same area of the scene as the current mouse view window).
This is why the shape should be drawn in
QGraphicsView::drawForeground()
, which is local to one view, not either inQGraphicsScene::drawForeground()
nor via placing aQGraphicsItem
on the scene (both of these would show the shape in all views observing that area of the scene, which I do not want.)As I say, I have this working well now :)
-
I was only thinking on a another way to to this.
-
@ollarch
:) No problem. You have really helpful getting me going on gfx scene + view.I am finding that
QGraphicsView::drawForeground()
is the best way to draw a temporary shape on (the top of) the given view (only). I came across that somewhere (but can't remember where) as the suggested way to achieve this best. -
Happy to help.
;D -
This post is deleted! -
Think on a CAD software. The schematic have to keep the units regardless of the zoom applied or the rotation of the view. You can rotate the view, zoom it, ... but when you ask the view to map the mouse position into scene coordinates, this coordinates will still be the same pixel position.