Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add widget to QScrollArea
Forum Update on Monday, May 27th 2025

How to add widget to QScrollArea

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 10.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pradeep Kumar
    wrote on 26 Apr 2017, 10:19 last edited by Pradeep Kumar
    #1

    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,

    Pradeep Kumar
    Qt,QML Developer

    K 1 Reply Last reply 26 Apr 2017, 10:34
    0
    • P Pradeep Kumar
      26 Apr 2017, 10:19

      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,

      K Offline
      K Offline
      koahnig
      wrote on 26 Apr 2017, 10:34 last edited by
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Pradeep Kumar
        wrote on 26 Apr 2017, 10:38 last edited by Pradeep Kumar
        #3

        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
        Qt,QML Developer

        N 1 Reply Last reply 26 Apr 2017, 11:08
        0
        • P Pradeep Kumar
          26 Apr 2017, 10:38

          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,

          N Offline
          N Offline
          Ni.Sumi
          wrote on 26 Apr 2017, 11:08 last edited by Ni.Sumi
          #4

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

          1 Reply Last reply
          2
          • P Offline
            P Offline
            Pradeep Kumar
            wrote on 26 Apr 2017, 11:22 last edited by
            #5

            i will try .

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

            Thanks,

            Pradeep Kumar
            Qt,QML Developer

            N 1 Reply Last reply 26 Apr 2017, 11:24
            0
            • P Pradeep Kumar
              26 Apr 2017, 11:22

              i will try .

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

              Thanks,

              N Offline
              N Offline
              Ni.Sumi
              wrote on 26 Apr 2017, 11:24 last edited by Ni.Sumi
              #6

              @Pradeep-Kumar

              Ofcourse, _here it is.

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

              P 1 Reply Last reply 26 Apr 2017, 11:27
              0
              • N Ni.Sumi
                26 Apr 2017, 11:24

                @Pradeep-Kumar

                Ofcourse, _here it is.

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

                P Offline
                P Offline
                Pradeep Kumar
                wrote on 26 Apr 2017, 11:27 last edited by
                #7

                @Ni.Sumi

                image is not attached, i cant see.

                Pradeep Kumar
                Qt,QML Developer

                N 1 Reply Last reply 26 Apr 2017, 11:27
                0
                • P Pradeep Kumar
                  26 Apr 2017, 11:27

                  @Ni.Sumi

                  image is not attached, i cant see.

                  N Offline
                  N Offline
                  Ni.Sumi
                  wrote on 26 Apr 2017, 11:27 last edited by
                  #8

                  @Pradeep-Kumar

                  https://postimg.org/image/i4uo77l23/

                  1 Reply Last reply
                  1
                  • P Offline
                    P Offline
                    Pradeep Kumar
                    wrote on 26 Apr 2017, 11:44 last edited by
                    #9

                    @Ni-Sumi

                    Thanks for the answer.

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

                    Thanks,

                    Pradeep Kumar
                    Qt,QML Developer

                    N Taz742T 2 Replies Last reply 26 Apr 2017, 11:48
                    0
                    • P Pradeep Kumar
                      26 Apr 2017, 11:44

                      @Ni-Sumi

                      Thanks for the answer.

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

                      Thanks,

                      N Offline
                      N Offline
                      Ni.Sumi
                      wrote on 26 Apr 2017, 11:48 last edited by Ni.Sumi
                      #10

                      @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()

                      1 Reply Last reply
                      5
                      • P Pradeep Kumar
                        26 Apr 2017, 11:44

                        @Ni-Sumi

                        Thanks for the answer.

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

                        Thanks,

                        Taz742T Offline
                        Taz742T Offline
                        Taz742
                        wrote on 27 Apr 2017, 10:20 last edited by
                        #11

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

                        Do what you want.

                        1 Reply Last reply
                        0

                        1/11

                        26 Apr 2017, 10:19

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved