I have successfully implemented an image viewer (for DICOM) in Qt. I see an image, and I can zoom in and out.
Now I want to see scroll bars if the image is too large to show when I zoom in.
I used the user interface. I posted a QScrollArea . Inside QLabel . VerticalScrollBarPolicy - ScrollBarAsNeeded. ScrollBarPolicy - ScrollBarAsNeeded
The problem is that it does not work. I am zooming in but the scrollbar is not showing.
Second attempt: using layout inside QScrollArea.
So now there is a QWidget between QScrollArea and QLabel: horizontal layout. Opened the same image, now I see a vertical scroll bar on the right. The image is stretched from left to right. When I enlarge the image, it gets the correct proportion.
BUT ... I zoom out and the scrollbar is the same even if I see the whole image. A horizontal scrollbar never appears.
Resizing QLabel does not seem to be affected. But if I resize QScrollArea (resize the main window), a horizontal scrollbar will appear.
I checked several numbers:
In QScrollArea
- Its size changes: the width is below 599 (why is this number? I do not see it anywhere) a horizontal panel appears.
- sizeHint () always returns the same values: 33x41
In QLabel
- Resizing, but it does not affect.
- sizeHint () always returns the same values: 560x1558
Here is the XML code from the user interface designer:
<widget class="QWidget" name="centralWidget"> <property name="autoFillBackground"> <bool>false</bool> </property> <layout class="QVBoxLayout" name="verticalLayout"> <property name="margin"> <number>0</number> </property> <item> <widget class="QScrollArea" name="scrollArea"> <property name="widgetResizable"> <bool>true</bool> </property> <property name="alignment"> <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set> </property> <widget class="QWidget" name="scrollAreaWidgetContents"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>637</width> <height>649</height> </rect> </property> <layout class="QHBoxLayout" name="horizontalLayout_2"> <item> <widget class="QLabel" name="miImagen"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="autoFillBackground"> <bool>true</bool> </property> <property name="scaledContents"> <bool>true</bool> </property> <property name="alignment"> <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set> </property> </widget> </item> </layout> </widget> </widget> </item> </layout> </widget>
What am I missing? Thanks.
source share