how do I set QScrollArea in my QWidget class constructor?
-
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 :

If I write scrollArea->setWidget(this); then my window just won't open.
How I can resolve this problem? -
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 :

If I write scrollArea->setWidget(this); then my window just won't open.
How I can resolve this problem?@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 ;)
-
@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 Fagot has marked this topic as solved on