1

Topic: Sheaf QGraphicsScene and QGraphicsView - scale

Kind time of days, dear colleagues! At me Qt 5.9.1 (Windows 7 + SP1). I apply sheaf QGraphicsScene and QGraphicsView. In my successor of class QGraphicsView I can change scale: void GraphTestView:: OnZoomIn () {scale (1.1, 1.1);} void GraphTestView:: OnZoomOut () {scale (1 / 1.1, 1 / 1.1);} There is quite logical question: how to count scale? To display to the user (in a status-bar): 50 % or 75 % or 100 % Besides, a question - as  to initiating scale (we assume to 100 %)? While I do so: void GraphTestView:: OnZoomToFit () {fitInView (scene ()-> sceneRect (), Qt:: KeepAspectRatio);} It is recommended here: https://stackoverflow.com/questions/108 … aphicsview However, I feel that NOT ABSOLUTELY that is required (by an estimation ' by eye ' after call OnZoomToFit it is necessary to fulfill it two more times OnZoomIn to achieve the visually same scale that was right at the beginning). Tried  so: void GraphTestView:: OnZoomToFit () {scale (frameSize ().width () / scene ()-> width (), frameSize ().height () / scene ()-> height ());} It changes scale, but again - NOT HOW it is required. That is, there is a double problem - correct count of scale and resetting to initiating scale. I googled on this subject much, but almost applicable (and working) councils not probably to find. In general - as all it to make correctly? In advance I thank for any thoughts!

2

Re: Sheaf QGraphicsScene and QGraphicsView - scale

Try so: setTransform (QTransform:: fromScale (x, y)); Then it will be possible not to calculate value of scale, and to install the necessary scale. For example, to get a slider of 10 % - 200 %, and to scale according to the installed value.

3

Re: Sheaf QGraphicsScene and QGraphicsView - scale

Hello, AlexGin, you wrote: AG> Kind time of days, dear colleagues! AG> Besides, a question - as  to initiating scale (we assume to 100 %)? AG> In advance I thank for any thoughts! Well, thought to use saveGeometry/restoreGeometry Or to save transform, and at  setTransform

4

Re: Sheaf QGraphicsScene and QGraphicsView - scale

Hello, AlexGin: From the project in the project I use such code. def _scale_view (self, factor): matrix = self.matrix () current_factor = matrix.map (1., 1.) [0] new_factor = matrix.scale (factor, factor).map (1., 1.) [0] if (0.07 <= new_factor <= 100. or current_factor <0.07 and new_factor> current_factor or new_factor> 100. and new_factor <current_factor): self.scale (factor, factor) self. _ set_zoom_text (new_factor) def _set_scale (self, scale): old_matrix = self.matrix () self.resetMatrix () self.translate (old_matrix.dx (), old_matrix.dy ()) self.scale (scale, scale) self. _ set_zoom_text (scale) On a mouse wheel: def wheelEvent (self, event): if event.modifiers () and Qt. ControlModifier: self. _ scale_view (2.0 ** (event.delta () / 240.0)) else: super (ImageView, self).wheelEvent (event) I Hope the thought is clear.

5

Re: Sheaf QGraphicsScene and QGraphicsView - scale

Hello, Lexerys, you wrote: L> L> setTransform (QTransform:: fromScale (x, y)); L> L> Then it will be possible not to calculate value of scale, and to install the necessary scale. For example, to get a slider of 10 % - 200 %, and to scale according to the installed value. +100500 thanks, are idea the worker!

6

Re: Sheaf QGraphicsScene and QGraphicsView - scale

Hello, Senyai, you wrote: S> I Hope thought it is clear. The thought in itself - is clear I it  for a C ++, I will test - then it will be visible, how much it the worker for my case. Anyway -  here result of sample of the given code!

7

Re: Sheaf QGraphicsScene and QGraphicsView - scale

Hello, Igore, you wrote: I> Well, thought to use saveGeometry/restoreGeometry  it changes only "type" geometry. A type/scene it affects a ratio as? Or does not affect at all? I> or to save transform, and at  setTransform Yes, here idea similar that the correct. In the given branch already resulted a specific example of implementation of this idea.

8

Re: Sheaf QGraphicsScene and QGraphicsView - scale

Hello, Senyai, you wrote: S> Hello, AlexGin: S> From the project in the project I use such code. S> S> def _scale_view (self, factor): S> matrix = self.matrix () S> current_factor = matrix.map (1., 1.) [0] S> new_factor = matrix.scale (factor, factor).map (1., 1.) [0] S> if (0.07 <= new_factor <= 100. or S> current_factor <0.07 and new_factor> current_factor or S> new_factor> 100. and new_factor <current_factor): S> self.scale (factor, factor) S> self. _ set_zoom_text (new_factor) S> def _set_scale (self, scale): S> old_matrix = self.matrix () S> self.resetMatrix () S> self.translate (old_matrix.dx (), old_matrix.dy ()) S> self.scale (scale, scale) S> self. _ set_zoom_text (scale) S> S> On a mouse wheel: S> S> def wheelEvent (self, event): S> if event.modifiers () and Qt. ControlModifier: S> self. _ scale_view (2.0 ** (event.delta () / 240.0)) S> else: S> super (ImageView, self).wheelEvent (event) S> S> I Hope the thought is clear. Many thanks, respected Senyai!!! All perfectly works! void GraphTestView:: ScaleView (qreal qFactor) {QMatrix matrix = this-> matrix (); qreal qrCurrTx = 0.0, qCurrTy = 0.0; matrix.map (1.0, 1.0, &qrCurrTx, &qCurrTy); double dbCurrentFactor = (qrCurrTx + qCurrTy) * 0.5; qreal qrNewTx = 0.0, qrNewTy = 0.0; matrix.scale (qFactor, qFactor).map (1.0, 1.0, &qrNewTx, &qrNewTy); double dbNewFactor = (qrNewTx + qrNewTy) * 0.5; if ((0.07 <= dbNewFactor <= 100.0) || ((dbCurrentFactor <0.07) && (dbNewFactor> dbCurrentFactor)) || ((dbCurrentFactor> 100.0) && (dbNewFactor <dbCurrentFactor))) {scale (qFactor, qFactor); emit SetZoomText (qFactor);}} void GraphTestView:: SetScale (qreal qrScale) {QMatrix old_matrix = this-> matrix (); resetMatrix (); translate (old_matrix.dx (), old_matrix.dy ()); scale (qrScale, qrScale); emit SetZoomText (qrScale);} And it is an output agent of messages from a mouse wheel (changes scale at holding CTRL on the keypad): void GraphTestView:: wheelEvent (QWheelEvent *event) {if (Qt:: KeyboardModifier:: ControlModifier == event-> modifiers ()) {qreal qrValue = pow (2.0, (event-> delta () / 240.0)); ScaleView (qrValue); emit SetZoomText (qrValue);} else {QGraphicsView:: wheelEvent (event);}}