Scrollbar in scroll area not working
I have a mainwindow of size 480 x 350. The central widget of the main window is a scroll area of dimension 190 x 220. The code for this is as below -
Code:
scrollArea
->setObjectName
(QString::fromUtf8("scrollArea"));
scrollArea
->setGeometry
(QRect(210,
20,
191,
221));
scrollArea->setWidgetResizable(true);
scrollAreaWidgetContents
= new QWidget();
scrollAreaWidgetContents->setObjectName(
QString::fromUtf8("scrollAreaWidgetContents"));
scrollAreaWidgetContents
->setGeometry
(QRect(0,
0,
187,
217));
scrollArea->setWidget(scrollAreaWidgetContents);
MainWindow->setCentralWidget(centralWidget);
I have a QLabel which is a child of the scrollarea. The code for this is as shown -
Code:
tmp->setText("Custom Label");
tmp
->setGeometry
(QRect( 300,
300,
60,
60));
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
So in the application, the scrollarea must show the scroll bars & user must be able to scroll to the qlabel , which will not be in the displayable area.
But when I run it, the scrollarea does not show the scrollbars & hence its not scrollable. How do i solve this issue? Am I doing something wrong here ?
Re: Scrollbar in scroll area not working
Quote:
The central widget of the main window is a scroll area
False, at least according to the code shown. The scroll area is in fact a child of the central widget.
Also, setGeometry() does its work relative to the parent widget, and you are setting the scrollAreaContents' geometry *before* it has a parent, for example.
All in all, I think you need to very carefully re-evaluate the parent-child hierarchy of your widgets and how it is manipulated in your code. I'm not sure if this makes a difference here, but it may be a good place to start, and certainly couldn't hurt.
1 Attachment(s)
Re: Scrollbar in scroll area not working
I have changed my mainwindow constructor as show -
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
scrollArea
->setObjectName
(QString::fromUtf8("scrollArea"));
scrollArea
->setGeometry
(QRect(210,
20,
191,
221));
scrollArea->setWidgetResizable(true);
scrollAreaWidgetContents
= new QWidget();
scrollAreaWidgetContents
->setObjectName
(QString::fromUtf8("scrollAreaWidgetContents"));
scrollAreaWidgetContents
->setGeometry
(QRect(0,
0,
187,
217));
scrollArea->setWidget(scrollAreaWidgetContents);
this->setCentralWidget( scrollArea );
tmp->setText("label");
tmp
->setGeometry
(QRect(50,
50,
2000,
2000));
tmp->show();
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
}
MainWindow::~MainWindow()
{
delete ui;
}
Here ui is a designer form created using QtDesigner. The ui file is attached.
Attachment 5120
When i run this application, the label is not shown nor are the scrollbars visible. How should I proceed ?