So, this is my problem: I have this very big image, and I want to show only a specific part of it. After the user pressing a specific key I want the image to move, showing another part of it. The transition from one part of the image to another have to be smooth, animated.
I tried using a QLabel to show the image but it always shows the center of the image, and I do not really know how to make the animation. What would you guys suggest?
Interesting question. Here is something I just tested and seems to work.
Add a
QGraphicsViewwith dimensions the dimensions of the part of the image you want to display, eg 100×100. Create aQGraphicsSceneand add it to the view:Now add your image into the scene. In my case I has an image in my resource file. The trick is to set the
sceneRectto the position you want to display. I wanted to display an 100×100 part of the image starting from 0,300 :In order to test the smooth moving I added a button which when clicked is triggering a slot called move. This slot simply updates the
sceneRect. In my simple example I just move the image 100 pixels right. In a real world scenario you could also move it diagonally or vertically and check the image limits.Notice the
currentXvariable. It is nothing more than the last image position. Also we must call theprocessEventsin order to “see” the image moving smoothly.