Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. how do I set QScrollArea in my QWidget class constructor?

how do I set QScrollArea in my QWidget class constructor?

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
3 Posts 2 Posters 328 Views 1 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.
  • F Offline
    F Offline
    Fagot
    wrote on last edited by
    #1

    Hello! I am new in QT.
    I have items in QGridLayout created in the widget, I also need to put a QScrollArea across the height of my window there, but it is created in the upper left corner if I use this code, without scrollArea->setWidget(this);:

    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
        QGridLayout *grid = new QGridLayout(this);
        grid->setSpacing(2);
        QList<QString> values({ "1", "2", "3", "4"});
        int pos = 0;
        for (int i=0; i<2; i++) {
            for (int j=0; j<2; j++) {
    
                QPushButton *btn = new QPushButton(values[pos], this);
                btn->setFixedSize(40, 40);
                grid->addWidget(btn, i, j);
                pos++;
            }
        }
        setLayout(grid);
        QScrollArea *scrollArea = new QScrollArea(this);
        scrollArea->setWidgetResizable(true);
         //scrollArea->setWidget(this);
        scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    }
    

    I will have this : photo_5212938847132959774_x.jpg

    If I write scrollArea->setWidget(this); then my window just won't open.
    How I can resolve this problem?

    M 1 Reply Last reply
    0
    • F Fagot

      Hello! I am new in QT.
      I have items in QGridLayout created in the widget, I also need to put a QScrollArea across the height of my window there, but it is created in the upper left corner if I use this code, without scrollArea->setWidget(this);:

      Widget::Widget(QWidget *parent)
          : QWidget(parent)
          , ui(new Ui::Widget)
      {
          ui->setupUi(this);
          QGridLayout *grid = new QGridLayout(this);
          grid->setSpacing(2);
          QList<QString> values({ "1", "2", "3", "4"});
          int pos = 0;
          for (int i=0; i<2; i++) {
              for (int j=0; j<2; j++) {
      
                  QPushButton *btn = new QPushButton(values[pos], this);
                  btn->setFixedSize(40, 40);
                  grid->addWidget(btn, i, j);
                  pos++;
              }
          }
          setLayout(grid);
          QScrollArea *scrollArea = new QScrollArea(this);
          scrollArea->setWidgetResizable(true);
           //scrollArea->setWidget(this);
          scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
      }
      

      I will have this : photo_5212938847132959774_x.jpg

      If I write scrollArea->setWidget(this); then my window just won't open.
      How I can resolve this problem?

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @Fagot said in how do I set QScrollArea in my QWidget class constructor?:

      If I write scrollArea->setWidget(this); then my window just won't open.

      Hi,
      You have to create another widget,
      an easy way would be to inherit from QScrollArea directly:

      Widget(QWidget *parent) : QScrollArea(parent)
      {
          setWidgetResizable(true);
          QGridLayout *grid = new QGridLayout;
          grid->setSpacing(10);
          QWidget* w=new QWidget;
          w->setLayout(grid);
          setWidget(w);
      
          for (int i=0; i<20; i++) {
              for (int j=0; j<2; j++) {
                  QPushButton *btn = new QPushButton(QString("%1-%2").arg(i).arg((j)));
                  btn->setFixedSize(80, 40);
                  grid->addWidget(btn, i, j);
              }
          }
      }
      

      I get rid of setupUi() because I think it does nothing ;)

      F 1 Reply Last reply
      1
      • M mpergand

        @Fagot said in how do I set QScrollArea in my QWidget class constructor?:

        If I write scrollArea->setWidget(this); then my window just won't open.

        Hi,
        You have to create another widget,
        an easy way would be to inherit from QScrollArea directly:

        Widget(QWidget *parent) : QScrollArea(parent)
        {
            setWidgetResizable(true);
            QGridLayout *grid = new QGridLayout;
            grid->setSpacing(10);
            QWidget* w=new QWidget;
            w->setLayout(grid);
            setWidget(w);
        
            for (int i=0; i<20; i++) {
                for (int j=0; j<2; j++) {
                    QPushButton *btn = new QPushButton(QString("%1-%2").arg(i).arg((j)));
                    btn->setFixedSize(80, 40);
                    grid->addWidget(btn, i, j);
                }
            }
        }
        

        I get rid of setupUi() because I think it does nothing ;)

        F Offline
        F Offline
        Fagot
        wrote on last edited by
        #3

        @mpergand Thank You so much! You saved me a lot of time :))))

        1 Reply Last reply
        0
        • F Fagot has marked this topic as solved on

        • Login

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