QLabel在QScrollArea中显示图像 [英] QLabel showing an image inside a QScrollArea
问题描述
我在Qt中成功实现了一个图像查看器(用于DICOM)。
我可以看到图像,我可以正确放大和缩小。
I've successfully implemented a Image Viewer (for DICOM) in Qt. I can see the image and I can zoom in and out correctly.
现在,如果图像太大而无法显示,我想查看滚动条我放大了。
Now, I want to see scroll bars if the image is too big to show when I zoom in.
我使用过UI。我放置了一个 QScrollArea 。在里面, QLabel 。
verticalScrollBarPolicy 是 ScrollBarAsNeeded 。
horizontalScrollBarPolicy 是 ScrollBarAsNeeded 。
I've used the UI. I placed a QScrollArea. Inside, the QLabel. The verticalScrollBarPolicy is ScrollBarAsNeeded. The horizontalScrollBarPolicy is ScrollBarAsNeeded.
问题是:它不起作用。我放大了,但没有滚动条出现。
The problem is: it doesn't work. I zoom in, but no scrollbar appears.
所以现在QScrollArea和QLabel之间有一个 QWidget :水平布局。
打开相同的图像,现在我可以看到右侧的垂直滚动条。图像从左向右拉伸。当我缩放图像得到正确的比例。
So now there's a QWidget between the QScrollArea and the QLabel: a horizontal layout. Opened the same image, now I can see a vertical scroll bar on the right. The image is streched from left to right. When I zoom the image gets its correct proportion.
但是......我缩小并且滚动条是相同的,即使我可以看到整个图像。水平滚动条永远不会出现。
BUT... I zoom out and the scroll bar is the same, even if I can see the whole image. The horizontal scroll bar never appears.
调整QLabel大小似乎不会影响。但是,如果我调整QScrollArea的大小(调整主窗口的大小),则会出现水平滚动条。
Resizing the QLabel doesn't seem to affect. But if I resize the QScrollArea (resizing the main window) the horizontal scroll bar appears.
- 它的尺寸变化:低于599宽度(为什么这个数字?我在任何地方都看不到它)出现水平条。
- sizeHint()始终返回相同的值:33x41
- Its size changes: below 599 width (why this number? I can't see it anywhere) the horizontal bar appears.
- sizeHint() always returns the same values: 33x41
- 尺寸发生变化,但不会影响。
- sizeHint()始终返回相同的值:560x1558
- The dimensions change, but that doesn't affect.
- sizeHint() always returns the same values: 560x1558
以下是XML中的代码来自UI设计师:
Here is the code in XML from the UI 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>
我缺少什么?谢谢。
推荐答案
我知道这是一篇旧帖子 - 但如果你或任何人仍有问题,可能会帮助将 QScrollArea :: widgetResizable
设置为false。
I know this is an old post - but in case you or anyone is still having a problem, it might help to set QScrollArea::widgetResizable
to false.
至少,当我尝试类似的东西时,我的垂直滚动条始终被禁用(即使我将可滚动窗口小部件的大小设置为高于视口的高度),直到我将其设置为false。
At least, when I tried a similar thing, my vertical scrollbar was always disabled (even though I set the size of the scrollable widget to have a height larger than the viewport) until I set this to false.
当它为真时,我认为它会更新可滚动小部件的大小,因此不需要滚动条。这允许你做我在猜测的例子中做的事情,并实现一个拉伸到适合的功能。 (实际上我正在尝试做的是适合宽度,只有一个垂直滚动条)
When it's true, I think it updates the size of the scrollable widget, thus the scrollbars should not be needed. This allows you to do what it does in the example I guess, and implement a stretch-to-fit function. (actually what I'm trying to do is fit to width, with just a vertical scroll bar)
这篇关于QLabel在QScrollArea中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!