How to make a custom class that can be put in a layout.
-
Hello
I'm just getting familiar with Qt and GUI programming in general.
I need to write a class that from which I can create an instance of and then put it in a layout in the main window. The class would be using QScrollArea which should expand to fill the slot in the layout where it is placed.
I would be using it about like this.
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); para = new ParameterAreaWidget(ui->centralwidget); ui->verticalLayout->addWidget(para); }
But how to make such a class? I have understood that it has to derive from QWidget so that it can be placed inside a layout.
Im confused about what is the role of the base class and what has to be the parent of what in order for it to work.
Can someone give me a rough outline what the class needs to be so that it can create the scroll area and then fill the slot it the layout.
I would think that the parameterWidget should the child of ui->centralWidget, and added to the layout of the main window. Inside the class. Should the parameterWidget base class instance be a child of the centralWidget or how? What about all the scrollarea and other stuff in the class, should they be children of the base class instance. I guess that it really tells how little I know about this.
I'm aiming for something like this. It the actual case those parameters would be textboxes, comboboxes, checkboxes and so on. In the code, its added to vertical layout but actually it would be like this.
What I got now is this. The ScrollArea in the top corner does not go into the layoyt.
parameterwidget.h
#ifndef PARAMETERAREAWIDGET_H #define PARAMETERAREAWIDGET_H #include <QScrollArea> #include <QVBoxLayout> #include <QWidget> #include <QPushButton> class ParameterAreaWidget : public QWidget { Q_OBJECT public: ParameterAreaWidget(QWidget* parent); private: QScrollArea *scrollArea; QVBoxLayout *verticalLayout2; QWidget *scrollAreaWidgetContents; }; #endif // PARAMETERAREAWIDGET_H
parameterwidget.cpp
#include "parameterareawidget.h" ParameterAreaWidget::ParameterAreaWidget(QWidget* parent) : QWidget(parent) { scrollArea = new QScrollArea(parent); scrollArea->setObjectName(QString::fromUtf8("scrollArea")); scrollArea->setWidgetResizable(true); scrollAreaWidgetContents = new QWidget(); scrollAreaWidgetContents->setObjectName(QString::fromUtf8("scrollAreaWidgetContents")); scrollAreaWidgetContents->setGeometry(QRect(0, 0, 682, 660)); verticalLayout2 = new QVBoxLayout(scrollAreaWidgetContents); verticalLayout2->setObjectName(QString::fromUtf8("verticalLayout")); for (int i = 0; i<100; i++) { QPushButton *pushButton = new QPushButton("PushButton " +QString::number(i),scrollAreaWidgetContents); pushButton->setObjectName(QString::fromUtf8("pushButton")); verticalLayout2->addWidget(pushButton); } scrollArea->setWidget(scrollAreaWidgetContents); }
-
Hello
I'm just getting familiar with Qt and GUI programming in general.
I need to write a class that from which I can create an instance of and then put it in a layout in the main window. The class would be using QScrollArea which should expand to fill the slot in the layout where it is placed.
I would be using it about like this.
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); para = new ParameterAreaWidget(ui->centralwidget); ui->verticalLayout->addWidget(para); }
But how to make such a class? I have understood that it has to derive from QWidget so that it can be placed inside a layout.
Im confused about what is the role of the base class and what has to be the parent of what in order for it to work.
Can someone give me a rough outline what the class needs to be so that it can create the scroll area and then fill the slot it the layout.
I would think that the parameterWidget should the child of ui->centralWidget, and added to the layout of the main window. Inside the class. Should the parameterWidget base class instance be a child of the centralWidget or how? What about all the scrollarea and other stuff in the class, should they be children of the base class instance. I guess that it really tells how little I know about this.
I'm aiming for something like this. It the actual case those parameters would be textboxes, comboboxes, checkboxes and so on. In the code, its added to vertical layout but actually it would be like this.
What I got now is this. The ScrollArea in the top corner does not go into the layoyt.
parameterwidget.h
#ifndef PARAMETERAREAWIDGET_H #define PARAMETERAREAWIDGET_H #include <QScrollArea> #include <QVBoxLayout> #include <QWidget> #include <QPushButton> class ParameterAreaWidget : public QWidget { Q_OBJECT public: ParameterAreaWidget(QWidget* parent); private: QScrollArea *scrollArea; QVBoxLayout *verticalLayout2; QWidget *scrollAreaWidgetContents; }; #endif // PARAMETERAREAWIDGET_H
parameterwidget.cpp
#include "parameterareawidget.h" ParameterAreaWidget::ParameterAreaWidget(QWidget* parent) : QWidget(parent) { scrollArea = new QScrollArea(parent); scrollArea->setObjectName(QString::fromUtf8("scrollArea")); scrollArea->setWidgetResizable(true); scrollAreaWidgetContents = new QWidget(); scrollAreaWidgetContents->setObjectName(QString::fromUtf8("scrollAreaWidgetContents")); scrollAreaWidgetContents->setGeometry(QRect(0, 0, 682, 660)); verticalLayout2 = new QVBoxLayout(scrollAreaWidgetContents); verticalLayout2->setObjectName(QString::fromUtf8("verticalLayout")); for (int i = 0; i<100; i++) { QPushButton *pushButton = new QPushButton("PushButton " +QString::number(i),scrollAreaWidgetContents); pushButton->setObjectName(QString::fromUtf8("pushButton")); verticalLayout2->addWidget(pushButton); } scrollArea->setWidget(scrollAreaWidgetContents); }
@Jari-Koivuluoma
Hi,
Derive your class from QScrollArea instead of QWidget.