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 Updated to NodeBB v4.3 + New Features

How to add widget to QScrollArea

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 10.4k Views 3 Watching
  • 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.
  • Pradeep KumarP 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

    alt text

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

    Thanks,

    K Offline
    K Offline
    koahnig
    wrote on 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
    • Pradeep KumarP Offline
      Pradeep KumarP Offline
      Pradeep Kumar
      wrote on 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

      Ni.SumiN 1 Reply Last reply
      0
      • Pradeep KumarP 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,

        Ni.SumiN Offline
        Ni.SumiN Offline
        Ni.Sumi
        wrote on 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
        • Pradeep KumarP Offline
          Pradeep KumarP Offline
          Pradeep Kumar
          wrote on 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

          Ni.SumiN 1 Reply Last reply
          0
          • Pradeep KumarP Pradeep Kumar

            i will try .

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

            Thanks,

            Ni.SumiN Offline
            Ni.SumiN Offline
            Ni.Sumi
            wrote on last edited by Ni.Sumi
            #6

            @Pradeep-Kumar

            Ofcourse, _here it is.

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

            Pradeep KumarP 1 Reply Last reply
            0
            • Ni.SumiN Ni.Sumi

              @Pradeep-Kumar

              Ofcourse, _here it is.

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

              Pradeep KumarP Offline
              Pradeep KumarP Offline
              Pradeep Kumar
              wrote on last edited by
              #7

              @Ni.Sumi

              image is not attached, i cant see.

              Pradeep Kumar
              Qt,QML Developer

              Ni.SumiN 1 Reply Last reply
              0
              • Pradeep KumarP Pradeep Kumar

                @Ni.Sumi

                image is not attached, i cant see.

                Ni.SumiN Offline
                Ni.SumiN Offline
                Ni.Sumi
                wrote on last edited by
                #8

                @Pradeep-Kumar

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

                1 Reply Last reply
                1
                • Pradeep KumarP Offline
                  Pradeep KumarP Offline
                  Pradeep Kumar
                  wrote on 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

                  Ni.SumiN Taz742T 2 Replies Last reply
                  0
                  • Pradeep KumarP Pradeep Kumar

                    @Ni-Sumi

                    Thanks for the answer.

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

                    Thanks,

                    Ni.SumiN Offline
                    Ni.SumiN Offline
                    Ni.Sumi
                    wrote on 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
                    • Pradeep KumarP Pradeep Kumar

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

                      • Login

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