dsyleixa123
Posts: 1435
Joined: Mon Jun 11, 2018 11:22 am

qt5 creator: how to disable auto-focussing of a QGraphicScene?

Fri May 14, 2021 5:21 pm

hi
as to qt5 creator: how to disable auto-focussing of a QGraphicScene?
My scene (1000x1000) is bigger than the form canvas (640x640), so I get automatically scroll bars horiz+vert,
but although I can move the view there and back, the program automatically shifts to elsewhere immediately after each redrawing so that I cannot get an arbitrary constant field of view -
how can I fix that?

Heater
Posts: 18194
Joined: Tue Jul 17, 2012 3:02 pm

Re: qt5 creator: how to disable auto-focussing of a QGraphicScene?

Sat May 15, 2021 7:19 am

Once again I have to refer you to this page in the Qt documentation about the Graphics View Framework:
https://doc.qt.io/qt-5/graphicsview.html

Note for example:

"Using its transformation matrix, QGraphicsView::transform(), the view can transform the scene's coordinate system. This allows advanced navigation features such as zooming and rotation. For convenience, QGraphicsView also provides functions for translating between view and scene coordinates: QGraphicsView::mapToScene() and QGraphicsView::mapFromScene()."


and

"Zooming and rotating

QGraphicsView supports the same affine transformations as QPainter does through QGraphicsView::setMatrix(). By applying a transformation to the view, you can easily add support for common navigation features such as zooming and rotating."


See the QGraphicsView methods: scale(), rotate(), translate() and others here: https://doc.qt.io/qt-5/qgraphicsview.html
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1435
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: how to disable auto-focussing of a QGraphicScene?

Sat May 15, 2021 7:45 am

sorry, I have already read that but it's too vague and too unspecific how exactly to do that.
"the view can transform the scene's coordinate system. This allows advanced navigation features such as zooming and rotation. For convenience, QGraphicsView also provides functions for translating...supports the same affine transformations":

That is no actual specific usable code.

So which is the actual code to sort of "freeze" the view field also if the scene becomes completely cleared and redrawed by the program repeatedly:

Code: Select all

void MainWindow::onUpdateTime() {
      scene->clear();
      outlinePen.setWidth(1);
      rectangle = scene->addRect( 1, 1, GOLscrWidth+frame, GOLscrHeight+frame, outlinePen, whiteBrush);
      //... 
      }
i.e., the view field is just set by the user, using just the scroll bars and this current setting should be kept -
so the program must stop always overwriting this scrollbar settings and stop always jumping and shifting to the outmost possible middle-right view field repeatedly.

if you wish you may test the unwanted behavior by this project code:
https://github.com/dsyleixa/RaspberryPi ... qt/GoL_004

https://github.com/dsyleixa/RaspberryPi ... nshot.jpeg
the scrollbars you see in the screenshot are actually unfunctional (always immediately re-shifting as described)

Heater
Posts: 18194
Joined: Tue Jul 17, 2012 3:02 pm

Re: qt5 creator: how to disable auto-focussing of a QGraphicScene?

Sat May 15, 2021 9:03 am

Sadly I have enough problems with my own code today to be spending any time debugging other peoples.

I'm not sure what is not clear about this page: https://doc.qt.io/qt-5/qgraphicsview.html It clearly lists all the methods on a view and tells what they do.

For example:
void QGraphicsView::translate(qreal dx, qreal dy)

Translates the current view transformation by (dx, dy).
I suggest you try some experiments. For example, wherever it is you are creating your view and scene add a call to the "translate(x, y)" method of your view and see what happens. Use some constants like 100, 200 for x and y.

Try similar experiments with rotate() and scale() etc.

The fact that you say the view is being repeatedly cleared and redrawn disturbs me. One should only need to create the view and scene once and then draw items in it, remove items, move items around, etc. Then one can set whatever view translation, scaling, rotation etc once when they are first created.
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1435
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: how to disable auto-focussing of a QGraphicScene?

Sat May 15, 2021 9:25 am

no, I have to clear and redraw all and everything because the dots / rectangles are never moved but calculated anew from the environmental neighbour dots (Conway's Game of Life):
From step to step some dots will suddenly vanish and some other dots will suddenly appear where nothing existed before.
(It's almost like if you want to put an unknown random number of new dots at random coordinates, and then in either next step put a different unknown random number of new dots at different random coordinates out of nowhere, and all old dots must be erased before.

OTOH, I don't understand at all what "transform" or "translate" means with respect to view fields (I don't "transform" or "translate" graphic objects, I just want to let the user move the view field by scroll bars and then temporarily make the view field freeze / keep it durable, stable).
Especially I cannot use "some constants like 100, 200" because the view field is moved and adjusted by the user via moving the scroll bars (or how should I retrieve the scrollbar settings from the previous user-scroll-bar move action and then perhaps pass them to the new scenes by their new scroll bars?).

Heater
Posts: 18194
Joined: Tue Jul 17, 2012 3:02 pm

Re: qt5 creator: how to disable auto-focussing of a QGraphicScene?

Sat May 15, 2021 10:49 am

dsyleixa123 wrote:
Sat May 15, 2021 9:25 am
OTOH, I don't understand at all what "transform" or "translate" means with respect to view fields (I don't "transform" or "translate" graphic objects, I just want to let the user move the view field by scroll bars and then temporarily make the view field freeze / keep it durable, stable).
"Translate" and "Transform" are terms used all over graphics programming.

"Translate" just means move. Move in the x direction, the y direction or both. Or even the z direction in 3D graphics.

Let's say you draw a square from lines joining a list of points: (0, 0), (0,1), (1, 1), (1, 0), (0,0)

Now suppose you want to move that square up by 3 and to the right by 5. What do you do? You add 3 to all the y coordinates, add 5 to all the x coordinates and draw lines between those adjusted coordinates: (0+5, 0+3), (0+5, 1+3), (1+5, 1+3), (1+5, 0+3), (0+5, 0+3) = (5, 3), (5, 4), (6, 4), (6, 3), (5, 3)

So "translate" just means adding some offset to all your coordinates, which moves your object.

Similarly "scale" just means magnify or reduce. What if you want to make your square bigger? Say twice as big? Well, just multiply all the coordinates by 2. Here the scale factor is 2. It cpu of course be anything.

"Transform" is just a general term for any of those operations or combinations of of them does at the same time. For example one could move and magnify that square (translate and scale) by using a transform that both multiplies all the coordinates baby a scale factor and adds an offset.

Often transforms are expressed as matrices. Translation matrix, scale matrix, rotation matrix. Multiplying ones coordinates by such a matrix. can move, scale and rotate and object all in one go.

dsyleixa123 wrote:
Sat May 15, 2021 9:25 am
Especially I cannot use "some constants like 100, 200" because the view field is moved and adjusted by the user via moving the scroll bars
My suggestion to use such constants was in the experiment I was suggesting you do. Simply to see what happens when you make those calls. Not in the final code, which will obviously need some variable parameters there.

I don't get the point about those life cells. You could just fill the grid up with cells and set them hidden. Then show live ones and hide dead ones. Or just change their colours to indicative and dead. Then you are not endlessly creating and destroying scenes and cells.
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1435
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: how to disable auto-focussing of a QGraphicScene?

Sat May 15, 2021 11:40 am

thank you, I'll have to think over it, but I haven't got it all yet, nonetheless monitoring and maintaining all single dots (perhaps millions of them) to set either one visible or unvisible appears to me a little overdrawn, tbh.
Additionally, as stated, I do not modify (translate, transform) any graph items or objects, they all stay how big and where located as they are, so these methods woudn't make any sense in my program.
OTOH I meanwhile found a code bug, there was an uncommented centerOn() command (which actually had to be out-commented), I think that had caused the main issue.
https://github.com/dsyleixa/RaspberryPi ... nshot.jpeg

Heater
Posts: 18194
Joined: Tue Jul 17, 2012 3:02 pm

Re: qt5 creator: how to disable auto-focussing of a QGraphicScene?

Sat May 15, 2021 12:44 pm

dsyleixa123 wrote:
Sat May 15, 2021 11:40 am
thank you, I'll have to think over it, but I haven't got it all yet, nonetheless monitoring and maintaining all single dots (perhaps millions of them) to set either one visible or unvisible appears to me a little overdrawn, tbh.
Yes, perhaps keeping all possible cells as items is a little memory heavy. I would not worry about it for the odd million or so.

The next best approach is to create the scene and view once. Then repeatedly draw all the live cells, clear the scene, draw the live cells...
dsyleixa123 wrote:
Sat May 15, 2021 11:40 am
Additionally, as stated, I do not modify (translate, transform) any graph items or objects, they all stay how big and where located as they are, so these methods woudn't make any sense in my program.
That sounds right. I was not suggesting you translate/transform any items in the scene. My suggestion was to translate/transform the scene with respect to the view. The methods I pointed out operate on the view not any particular items. This way the view can pan and zoom around the scene. No movement of individual items required.

Think of it like walking around a room while looking out the window. Notice how all the stuff in the world outside moves around with respect the window. The window is like the Qt view, the world outside is like the Qt scene. Only one thing moves, you, and all the world gets translated through the window differently. Similarly when you apply a transform to a Qt view.
dsyleixa123 wrote:
Sat May 15, 2021 11:40 am
OTOH I meanwhile found a code bug, there was an uncommented centerOn() command (which actually had to be out-commented), I think that had caused the main issue.
https://github.com/dsyleixa/RaspberryPi ... nshot.jpeg
Ah, that would do it.
Memory in C++ is a leaky abstraction .

Return to “C/C++”