How to add widget to QScrollArea
-
wrote on 26 Apr 2017, 10:19 last edited by Pradeep Kumar
Hi,
I am trying to add widget to QScrollarea,
i am getting scrollbars, but widget is not visible in QScrollarea, but it is behind QScrollarea,Sample code,
m_pQWidget = new QWidget; m_pQWidget->setFixedSize(2000,2000); m_pQScrollArea = new QScrollArea; m_pQScrollArea->setWidgetResizable(true); m_pQScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); m_pQScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); m_pQVBoxLayoutMain = new QVBoxLayout; m_pQScrollArea->setWidget(m_pQWidget); m_pQVBoxLayoutMain->addWidget(m_pQScrollArea); setLayout(m_pQVBoxLayoutMain);
And the output, is provided in the link, the widget is behind scrollarea,
https://i.imgsafe.org/0740452093.png
can u provide guidance, on how can i achieve this?.
Thanks,
-
Hi,
I am trying to add widget to QScrollarea,
i am getting scrollbars, but widget is not visible in QScrollarea, but it is behind QScrollarea,Sample code,
m_pQWidget = new QWidget; m_pQWidget->setFixedSize(2000,2000); m_pQScrollArea = new QScrollArea; m_pQScrollArea->setWidgetResizable(true); m_pQScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); m_pQScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); m_pQVBoxLayoutMain = new QVBoxLayout; m_pQScrollArea->setWidget(m_pQWidget); m_pQVBoxLayoutMain->addWidget(m_pQScrollArea); setLayout(m_pQVBoxLayoutMain);
And the output, is provided in the link, the widget is behind scrollarea,
https://i.imgsafe.org/0740452093.png
can u provide guidance, on how can i achieve this?.
Thanks,
wrote on 26 Apr 2017, 10:34 last edited byDid you try to populate your widget?
I had added recently QScrollArea using designer. The generated ui file has basically the steps you mention. However, before setting the widget with setWidget there are all the steps for population of the widget.
Hope this helps.
-
wrote on 26 Apr 2017, 10:38 last edited by Pradeep Kumar
Without scrollarea, i am getting the widget displayed,
but i want inside QScrollarea,How can i add QWidget inside QScrollArea, am i doing any wrong in the above code posted, any modifications needed?.
Thanks,
-
Without scrollarea, i am getting the widget displayed,
but i want inside QScrollarea,How can i add QWidget inside QScrollArea, am i doing any wrong in the above code posted, any modifications needed?.
Thanks,
wrote on 26 Apr 2017, 11:08 last edited by Ni.SumiI have tried in the sample program using your code . It worked , use some qDebug's to get exact info.
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QWidget* my = new QWidget(this); //Widget my->setFixedSize(2000, 2000); my->setStyleSheet("background-color: yellow "); QScrollArea* m_pQScrollArea = new QScrollArea; m_pQScrollArea->setWidget(my); m_pQScrollArea->setWidgetResizable(true); m_pQScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); m_pQScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); QVBoxLayout* m_pQVBoxLayoutMain = new QVBoxLayout; m_pQVBoxLayoutMain->addWidget(m_pQScrollArea); QWidget* central = new QWidget(this); central->setLayout(m_pQVBoxLayoutMain); setCentralWidget(central); }
Edit:
USe some back ground color for the Widget, it helps to know whether it got added or not. Currently , it si empty widget , not possible to see .
-
wrote on 26 Apr 2017, 11:22 last edited by
i will try .
can u post the image please, which you got the output @Ni.Sumi.
Thanks,
-
i will try .
can u post the image please, which you got the output @Ni.Sumi.
Thanks,
wrote on 26 Apr 2017, 11:24 last edited by Ni.Sumi -
wrote on 26 Apr 2017, 11:27 last edited by
image is not attached, i cant see.
-
image is not attached, i cant see.
wrote on 26 Apr 2017, 11:27 last edited by -
wrote on 26 Apr 2017, 11:44 last edited by
@Ni-Sumi
Thanks for the answer.
can u give me sample inheriting QWidget,
how can it be done.?.Thanks,
-
@Ni-Sumi
Thanks for the answer.
can u give me sample inheriting QWidget,
how can it be done.?.Thanks,
wrote on 26 Apr 2017, 11:48 last edited by Ni.SumiIs this way?
#include "widget.h" #include "ui_widget.h" #include <QVBoxLayout> #include <QScrollArea> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); QWidget* my = new QWidget(this); //Widget my->setFixedSize(2000, 2000); my->setStyleSheet("background-color: yellow "); QScrollArea* m_pQScrollArea = new QScrollArea; m_pQScrollArea->setWidget(my); m_pQScrollArea->setWidgetResizable(true); m_pQScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); m_pQScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); QVBoxLayout* m_pQVBoxLayoutMain = new QVBoxLayout; m_pQVBoxLayoutMain->addWidget(m_pQScrollArea); setLayout(m_pQVBoxLayoutMain); } Widget::~Widget() { delete ui; }
https://postimg.org/image/4bspfhlkf/
It's the same , in the place of
setCentralWidget()
, we needsetLAyout()
-
@Ni-Sumi
Thanks for the answer.
can u give me sample inheriting QWidget,
how can it be done.?.Thanks,
wrote on 27 Apr 2017, 10:20 last edited by@Pradeep-Kumar
I think the problem has been solved.
Mark as solved.
1/11