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 make a custom class that can be put in a layout.
Forum Updated to NodeBB v4.3 + New Features

How to make a custom class that can be put in a layout.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 163 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.
  • J Offline
    J Offline
    Jari Koivuluoma
    wrote on last edited by Jari Koivuluoma
    #1

    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.

    ParameterWidget.PNG

    What I got now is this. The ScrollArea in the top corner does not go into the layoyt.
    ParameterWidget2.PNG

    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);
    }
    
    
    M 1 Reply Last reply
    0
    • J Jari Koivuluoma

      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.

      ParameterWidget.PNG

      What I got now is this. The ScrollArea in the top corner does not go into the layoyt.
      ParameterWidget2.PNG

      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);
      }
      
      
      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      @Jari-Koivuluoma
      Hi,
      Derive your class from QScrollArea instead of QWidget.

      1 Reply Last reply
      1

      • Login

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