How to calculate the x value of the pixel based on scale changes



  • I have a QGraphicsScene and QGraphicsView. If i am pointing a pixel at X:100;y:100 at the scale(1,1). Then, what is the x,y co-ordinates of the pixel, when there is scale(0.8,1)? Is anyway to findout the logic behind this?



  • Did you try one of mapFromScene() function from QGraphicsView?



  • Sorry if i miss something but wouldn't it be the multiplication of the X (or Y) with its corresponding scale? So if X:100 and Y:100 with scale(1,1) wouldn't it be X': aX and Y':bY where scale(a, b)?

    That would result your requesting coordinates to be X:80, Y:100.

    Also in the above logic you have to add the offset of the view that is being scaled from the Coordinate origin.

    Isn't it?



  • Hi favoritas37, Can you please give some more detail. I tried with the logic of X’: aX and Y’:bY. But it is not working. What is the offset you mean?



  • I need one clarification.

    You have your image and you point with the mouse at X,Y. Then you keep the mouse to the same place but the image is scaled to (0.8,1). Now you point again at X,Y but you want to find which would be the coordinates of the image's pixel if it was not scaled?

    In this case you use the following: X' = X/a, Y'=Y/b

    The offset that i was talking about is in case the image is not printed at (X,Y)= (0,0) of the parent widget. So lets call that the image gets printed at position (x,y)=(c,d) where c and d are a random number. Then the above function for X' and Y' would look like this:

    X' = ((X-c)/a + c ), Y' = ((Y-d)/b + d)

    Any way, probably the first without the offset will be fine.



  • I will explain the actual scenario in detail below:

    I have a QGraphicsScene and QGraphicsView. Both objects has the width of 600. Now when i place a drawing on the QGraphicsScene, the width (itemsBoundingRect()) of the QGraphicsScene is increasedto 1400. In this case, i will show a manual Horizontal scroll bar used in viewing the image by scrolling.

    For this, i wrote the below logic:

    QRectF sceneRect = plotScene->itemsBoundingRect();
    plotScene->setSceneRect((value * ((sceneRect.width())/100)),0,600 ,350);

    The above logic is working fine, when it is Scale(1,1). But When i changed the Scale level, the scroll operation not working as expected. This is the actual problem.

    Note: For some reasons i am using the separate scroll bar and not the automatic one coming with QGraphicsView/Scene.



  • I still have a small problem trying to correlate your first question with your actual problem.

    The value variable i will suppose that is used with the division to 100 to create a percentage for the scaling factor?

    Why are you changing the X position of the plotScene? If you scale something normally you would change the width/height, not the x,y position of its.

    So ok, the scrollbar doesn't work as expected, but i don't get which is the expected case.

    Also have you looked warjan's suggestion about "mapFromScene()":http://qt-project.org/doc/qt-4.8/qgraphicsview.html#mapFromScene?



  • The Value variable holds the position of the scroll bar. It varies from 0 to 99. The below code is present inside the HScrollbar value change event:

    QRectF sceneRect = plotScene->itemsBoundingRect();
    plotScene->setSceneRect((value * ((sceneRect.width())/100)),0,600 ,350);

    If the ScrollBar position is 0, then the QGraphicsView should hold the QGraphicsScene data from
    the left edge of the GraphicsScene to the width of the view area.
    If the ScrollBar position is 99, then the QGraphicsView should hold the QGraphicsScene data from the right edge of the GraphicsScene to the width of the view area.

    Same kind of behavior, i am expecting for all scale levels. But it is not happening.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.