ScrollBars not showing up
-
I know this topic is similar to others I have found but I cannot get either vertical or horizontal scrollbars to show up when the imageViewport is bigger than the scrollArea. There are two of us learning Qt here and neither one of us have been able to figure this one out. Hopefully it is something simple. Any help much appreciated ...
Using Designer/Creator Qt 5.5.1, I have created a standard Qt Windows desktop application with a QMainWindow called EpoieReplacement, which contains a centralWidget, which contains a QscrollArea called scrollArea, which contains a QWidget called scrollAreaWidgetContents and a QLabel called imageViewport.
QMainWindow EpoieReplacement QWidget centralWidget QScrollAreascrollArea QWidget scrollAreaWidgetContents QLabel imageViewport
The scrollArea is set to ScrollBarAsNeeded for both v and h scrollbars.
Here is the pertinent code created by the Designer in the ui_EpoieReplacement.h file:
(No title/menu/tool/status bar code included here, as that all works fine)centralWidget = new QWidget(EpoieReplacement); centralWidget->setObjectName(QStringLiteral("centralWidget")); scrollArea = new QScrollArea(centralWidget); scrollArea->setObjectName(QStringLiteral("scrollArea")); scrollArea->setGeometry(QRect(0, 0, 981, 711)); scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); scrollArea->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow); scrollArea->setWidgetResizable(true); scrollAreaWidgetContents = new QWidget(); scrollAreaWidgetContents->setObjectName(QStringLiteral("scrollAreaWidgetContents")); scrollAreaWidgetContents->setGeometry(QRect(0, 0, 979, 709)); QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); sizePolicy.setHorizontalStretch(0); sizePolicy.setVerticalStretch(0); sizePolicy.setHeightForWidth(scrollAreaWidgetContents->sizePolicy().hasHeightForWidth()); scrollAreaWidgetContents->setSizePolicy(sizePolicy); imageViewport = new QLabel(scrollAreaWidgetContents); imageViewport->setObjectName(QStringLiteral("imageViewport")); imageViewport->setGeometry(QRect(0, 0, 1071, 221)); imageViewport->setScaledContents(true); scrollArea->setWidget(scrollAreaWidgetContents); EpoieReplacement->setCentralWidget(centralWidget);
Here is the pertinent code in the QMainWindow (EpoieReplacement) constructor:
ui->setupUi(this); ui->imageViewport = ui->imageViewport; ui->imageViewport->setBackgroundRole(QPalette::Base); ui->imageViewport->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); ui->imageViewport->setScaledContents(true); ui->scrollArea->setBackgroundRole(QPalette::Dark); ui->scrollArea->setWidgetResizable(true); ui->scrollArea->setWidget(ui->scrollAreaWidgetContents); setCentralWidget(ui->scrollArea);
-
Hi,
Your
imageViewport
widget is "floating" inscrollAreaWidgetContents
. You should put it in a layout that is set onscrollAreaWidgetContents
.You should take a look at the Image Viewer Example.
-
Thanks for the response, SGaist. The Image Viewer Example is the example I followed, except that it does not use Designer. Guess I'll have to figure out how to put the
imageViewport
widget in a layout that is set onscrollAreaWidgetContents
, unless someone can explain to me how to do that in Designer. -
It's explained in the Designer Layouts part of Designer's documentation.
-
Not sure you will be able to access this image url but it shows that both centralWidget and scrollAreaWidgetContents Widgets with grid layout icons, but both have a red circle with a line through it on top of the grid layout icon, and I don't know what that means.
-
No I can't see it however from your description it looks like you have no layout applied on the widget.
-
Thanks. I now have scroll bars which scroll the image up/down and left/right when the imageViewport is bigger than the scrollArea. Just need to figure out what to set to force the scroll bars to adjust when I resize the image and to disappear when the imageViewport is resized to be smaller than the scrollArea.
-
With the AsNeeded policy it should already be the case. Are you sure you are resizing the right element ?