QLabel shows image inside QScrollArea

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.

+4
source share
2 answers

I know this is an old post, but if you or anyone else has a problem, this can help set QScrollArea::widgetResizable to false.

At least when I tried a similar thing, my vertical scrollbar was always turned off (although I set the size of the scrollable widget so that the height was larger than the viewport) until I set it to false.

When this is true, I think it updates the size of the scrollable widget, so scrollbars are not needed. This allows you to do what it does in the example that I am assuming and implement the stretch function. (in fact, what I'm trying to do matches the width, only with a vertical scrollbar)

+3
source

Have you tried following the Qt scroll pane example ? If you use QLabel to display your image, then using QScrollArea is pretty much the standard way to achieve what you want. You use it like this (from the example):

  imageLabel = new QLabel; imageLabel->setBackgroundRole(QPalette::Base); imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); imageLabel->setScaledContents(true); scrollArea = new QScrollArea; scrollArea->setBackgroundRole(QPalette::Dark); scrollArea->setWidget(imageLabel); setCentralWidget(scrollArea); 

Then scaling is performed as follows:

  void ImageViewer::zoomIn() { scaleImage(1.25); } void ImageViewer::zoomOut() { scaleImage(0.8); } void ImageViewer::scaleImage(double factor) { Q_ASSERT(imageLabel->pixmap()); scaleFactor *= factor; imageLabel->resize(scaleFactor * imageLabel->pixmap()->size()); adjustScrollBar(scrollArea->horizontalScrollBar(), factor); adjustScrollBar(scrollArea->verticalScrollBar(), factor); zoomInAct->setEnabled(scaleFactor < 3.0); zoomOutAct->setEnabled(scaleFactor > 0.333); } void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor) { scrollBar->setValue(int(factor * scrollBar->value() + ((factor - 1) * scrollBar->pageStep()/2))); } 

You can, of course, better understand what is happening by looking at an example, but its essence. I think maybe the adjustScrollBar () function might be most useful to you.

Your last comment on the original post is correct, QScrollArea doesnโ€™t magically notice the resizing of its contents. See how this example uses adjustScrollBar () to compensate for this fact.

+1
source

All Articles