The simplest way is just to: create a QWidget, create a QLayout with that widget as parent and add other widgets to the layout:
//create a layout with parent
//widgets that will be contained in QWidget
layout->addWidget(label1); //add contained widgets to layout
layout->addWidget(label2);
w.show(); //show the widget
QWidget w;
//create a layout with parent
QVBoxLayout *layout = new QVBoxLayout(&w);
//widgets that will be contained in QWidget
QLabel* label1 = new QLabel("Label 1");
QLabel* label2 = new QLabel("Label 2");
layout->addWidget(label1); //add contained widgets to layout
layout->addWidget(label2);
w.show(); //show the widget
To copy to clipboard, switch view to plain text mode
Or another way is to derive a class from QWidget and contain the widgets into that class, this creates a better "encapsulated widget" that can be reused easier.
//MyWidget.h file
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
//forward declarations
class MyWidget
: public QWidget //inherit from QWidget{
Q_OBJECT
public:
MyWidget
(QWidget* parent
= 0);
//don't forget to pass the parent
private:
//contained widgets:
signals:
//MyWidget's signals....
public slots:
//MyWidget's slots example:
// void firstButtonClicked();
//...
};
#endif // MYWIDGET_H
//MyWidget.h file
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
//forward declarations
class QVBoxLayout;
class QHBoxLayout;
class QLabel;
class QPushButton;
class MyWidget : public QWidget //inherit from QWidget
{
Q_OBJECT
public:
MyWidget(QWidget* parent = 0); //don't forget to pass the parent
private:
//contained widgets:
QVBoxLayout *mainLayout;
QHBoxLayout *anotherLayout;
QLabel *firstLabel;
QLabel *secondLabel;
QPushButton *firstButton;
QPushButton *secondButton;
signals:
//MyWidget's signals....
public slots:
//MyWidget's slots example:
// void firstButtonClicked();
//...
};
#endif // MYWIDGET_H
To copy to clipboard, switch view to plain text mode
And in the constructor you initialize the pointers and add them to the layouts:
#include "mywidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
{
firstLabel
= new QLabel("first label");
secondLabel
= new QLabel("second label");
mainLayout->addWidget(firstLabel);
mainLayout->addWidget(firstButton);
mainLayout->addLayout(anotherLayout);
anotherLayout->addWidget(secondLabel);
anotherLayout->addWidget(secondButton);
}
#include "mywidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
MyWidget::MyWidget(QWidget* parent) : QWidget(parent)
{
mainLayout = new QVBoxLayout(this);
anotherLayout = new QHBoxLayout();
firstLabel = new QLabel("first label");
secondLabel = new QLabel("second label");
firstButton = new QPushButton("Fist button");
secondButton = new QPushButton("Second Button");
mainLayout->addWidget(firstLabel);
mainLayout->addWidget(firstButton);
mainLayout->addLayout(anotherLayout);
anotherLayout->addWidget(secondLabel);
anotherLayout->addWidget(secondButton);
}
To copy to clipboard, switch view to plain text mode
Bookmarks