0

I am new in Qt and am using qt creator for the GUI designing, now i have to make a dynamic form in which a label,line edit and a button are added for each feature like length,width,etc.However the features are not static there may be 2 feature sometime and other time there might be 6 depending upon the xml file.thus i want to make a widget which for each feature creates an instance.

I have made basic structure of grouped widget using this:

cal_widget.h

#ifndef CAL_WIDGET_H
#define CAL_WIDGET_H

#include <QWidget>
#include<QVBoxLayout>
#include<QPushButton>
#include<QLineEdit>
class cal_widget : public QWidget
{
    Q_OBJECT
public:
    explicit cal_widget(QWidget *parent = nullptr);
    QVBoxLayout* layout;
    QPushButton* btn;
    QLineEdit* ln1;
    QLineEdit* ln2;

signals:

public slots:
};

#endif // CAL_WIDGET_H

cal_widget.cpp

#include "cal_widget.h"

cal_widget::cal_widget(QWidget *parent) : QWidget(parent)
{
    layout = new QVBoxLayout();
    btn= new QPushButton();
    ln1 = new QLineEdit("mm");
    ln2 = new QLineEdit("pix");

    layout->addWidget(ln1);
    layout->addWidget(btn);
    layout->addWidget(ln2);
    this->setLayout(layout);
}

and calling the above widget in another form mainwidget.cpp

{
QVBoxLayout* vbox = new QVBoxLayout();
cal_widget* cal1 = new cal_widget(this);
cal_widget* cal2 = new cal_widget(this);
cal_widget* cal3 = new cal_widget(this);
cal_widget* cal4 = new cal_widget(this);
cal_widget* cal5 = new cal_widget(this);
vbox->addWidget(cal1);
vbox->addWidget(cal2);
vbox->addWidget(cal3);
vbox->addWidget(cal4);
vbox->addWidget(cal5);
ui->scrollArea->setLayout(vbox);
}

i am getting result as :this

how can i get desired result

vasu gupta
  • 305
  • 1
  • 3
  • 11
  • There are many such examples in the Qt documentation. SO is not a free programming service. Show what you have tried, and if at the time of trying you have problems we can help you. – eyllanesc Aug 22 '17 at 09:29
  • can you tell if i have to create a plugin or an application – vasu gupta Aug 22 '17 at 10:14
  • For your case and your inexperience I do not see necessary to create a plugin, read the qt tutorials and apply. – eyllanesc Aug 22 '17 at 12:18
  • If you know how to generate it statically (using QtDesigner), you can have a look at the generated code to see how it is translated by Qt into C++. – m7913d Aug 22 '17 at 20:15
  • read the qt tutorial for what?what exactly is it called?? – vasu gupta Aug 23 '17 at 05:15
  • @eyllanesc i m unable to find the example can u please tell be what exactly are they called – vasu gupta Aug 23 '17 at 13:01
  • @eyllanesc i have updated by post and have added what i have done till now can u please tell m what m i doing wrong and how to correct it – vasu gupta Aug 24 '17 at 04:38
  • Your code is almost perfect, what you need is to create a widget and place the vbox layout, and then just add that widget to QScrollArea – eyllanesc Aug 24 '17 at 04:56
  • Change `ui->scrollArea->setLayout(vbox);` to `QWidget *w = new QWidget(this); w->setLayout(vbox); ui->scrollArea->setWidget(w);` – eyllanesc Aug 24 '17 at 04:58

1 Answers1

2

DONE

cal_widget.h

#ifndef CAL_WIDGET_H
#define CAL_WIDGET_H

#include <QWidget>
#include<QGroupBox>
#include<QGridLayout>
#include<QPushButton>
#include<QLineEdit>
class cal_widget : public QGroupBox
{
    Q_OBJECT
public:
    explicit cal_widget(const QString& feature, QWidget *parent = nullptr);
    QGridLayout* layout;
    QPushButton* btn;
    QLineEdit* ln1;
    QLineEdit* ln2;

signals:

public slots:
};

#endif // CAL_WIDGET_H

cal_widget.cpp

#include "cal_widget.h"

cal_widget::cal_widget(const QString& feature, QWidget *parent) : QGroupBox(parent)
{
    this->setTitle(feature);
    layout = new QGridLayout();
    btn= new QPushButton("OK");
    ln1 = new QLineEdit("mm");
    ln2 = new QLineEdit("pix");

    layout->addWidget(ln1,0,0);
    layout->addWidget(ln2,0,1);
    layout->addWidget(btn,1,0,1,2);
    this->setLayout(layout);
}
vasu gupta
  • 305
  • 1
  • 3
  • 11