How to add widget to QScrollArea



  • 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

    alt text

    can u provide guidance, on how can i achieve this?.

    Thanks,


  • Moderators

    @Pradeep-Kumar

    Did 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.



  • 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,



  • @Pradeep-Kumar

    I 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 .



  • i will try .

    can u post the image please, which you got the output @Ni.Sumi.

    Thanks,



  • @Pradeep-Kumar

    Ofcourse, _here it is.

    ![alt text](0_1493205885882_upload-589493cd-f26e-45c0-88a7-0f3217b36bd4 image url)



  • @Ni.Sumi

    image is not attached, i cant see.





  • @Ni-Sumi

    Thanks for the answer.

    can u give me sample inheriting QWidget,
    how can it be done.?.

    Thanks,



  • @Pradeep-Kumar

    Is 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 need setLAyout()



  • @Pradeep-Kumar
    I think the problem has been solved.
    Mark as solved.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.