As seen in the pictures.
I have QWidget inside a QScrollArea. QWidget act as a render widget for cell image and some vector based contour data. User can performe zoom in/out and what simply happens is, it changes the QPainters scale and change the size of QWidget size accordinly.
Now I want to perform the zooming in/out on the point under the mouse. (like zooming action in GIMP). How to calculate the new positions of the scrollbars according to the zoom level ? Is it better to implement this using transformations without using a scrollarea?
You need to pick up the
wheelEvent()
on the QWidget, get the event.pos() and pass it into theQscrollArea.ensureVisible()
, right after scaling your QWidget.That should more or less produce what you want.
Will void
QScrollArea::ensureVisible(int x, int y, int xmargin = 50, int ymargin = 50)
do what you need?One solution could be to derive a new class from QScrollArea and reimplementing
wheelEvent
for example so that zooming is performed with the mouse wheel and at the current mouse cursor position.This method works by adjusting scroll bar positions accordingly to reflect the new zoom level. This means as long as there is no visible scroll bar, zooming does not take place under mouse cursor position. This is the behavior of most image viewer applications.