我正在尝试实现以下架构:
- QGroupBox --> 设置布局
- QVBoxLayout --> addWidget
- QScrollArea --> setWidget
- QWidget --> 设置布局
- QVBoxLayout --> addWidget
- 我的小部件(Mesures)
- QVBoxLayout --> addWidget
- QWidget --> 设置布局
- QScrollArea --> setWidget
- QVBoxLayout --> addWidget
我正在寻找的结果是有一个带有滚动区域的组框,里面有我所有的小部件。但是当我尝试实现这一点时,滚动区域中没有任何内容。
这是我所拥有的:http: //imgur.com/SgQkJ
编辑:这就是我删除滚动区域并直接将我的小部件放入 gorupbox http://imgur.com/iuTnV时所拥有的
这是我的代码:
测量.h
#ifndef MESURE_H
#define MESURE_H
#include <QWidget>
#include <QHBoxLayout>
#include <QToolButton>
#include <QLabel>
#include <QCheckBox>
#include <QString>
class Mesure : public QWidget
{
public:
Mesure(QString angle, QWidget *parent);
private:
QHBoxLayout *m_hlayoutMesure;
QToolButton *m_buttonClose;
QLabel *m_labelAngle;
QCheckBox *m_checkboxMesure;
};
#endif
测量.cpp
#include "Mesure.h"
Mesure::Mesure(QString angle, QWidget *parent) :
QWidget(parent),
m_hlayoutMesure(new QHBoxLayout(this)),
m_buttonClose(new QToolButton(this)),
m_labelAngle(new QLabel(angle, this)),
m_checkboxMesure(new QCheckBox(this))
{
m_buttonClose->setText("X");
m_labelAngle->setAlignment(Qt::AlignCenter);
m_hlayoutMesure->addStretch(1);
m_hlayoutMesure->addWidget(m_checkboxMesure);
m_hlayoutMesure->addWidget(m_labelAngle);
m_hlayoutMesure->addWidget(m_buttonClose);
m_hlayoutMesure->addStretch(1);
m_hlayoutMesure->setSpacing(10);
m_hlayoutMesure->setMargin(0);
QObject::connect(m_buttonClose, SIGNAL(clicked()), this, SLOT(close()));
}
PanneauMesure.h
#ifndef PANNEAUMESURE_H
#define PANNEAUMESURE_H
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QToolButton>
#include <QLabel>
#include <QCheckBox>
#include <QString>
#include <QPushButton>
#include <QSlider>
#include <QSpacerItem>
#include <QGroupBox>
#include <QScrollArea>
#include <vector>
#include "Mesure.h"
class PanneauMesure : public QWidget
{
Q_OBJECT
public:
PanneauMesure();
void add(Mesure *mesure);
public slots:
void remiseAZero();
private:
QVBoxLayout *m_vlayoutTotal;
QGroupBox *m_groupboxMesures;
QVBoxLayout *m_vlayoutGroupBoxMesures;
QScrollArea *m_scrollareaMesures;
QWidget *m_widgetMesures;
QVBoxLayout *m_vlayoutMesures;
QGroupBox *m_groupboxOptions;
QVBoxLayout *m_vlayoutOptions;
QCheckBox *m_checkboxAfficherMesures;
QPushButton *m_buttonRAZ;
QWidget *m_widgetAlpha;
QHBoxLayout *m_hlayoutAlpha;
QLabel *m_labelAlphaText;
QLabel *m_labelAlphaValeur;
QSlider *m_sliderAlpha;
};
#endif
PanneauMesure.cpp
#include "PanneauMesure.h"
PanneauMesure::PanneauMesure() :
QWidget(),
m_vlayoutTotal(new QVBoxLayout(this)),
m_groupboxMesures(new QGroupBox("Mesures", this)),
m_vlayoutGroupBoxMesures(new QVBoxLayout(m_groupboxMesures)),
m_scrollareaMesures(new QScrollArea(m_groupboxMesures)),
m_widgetMesures(new QWidget(m_groupboxMesures)),
m_vlayoutMesures(new QVBoxLayout(m_widgetMesures)),
m_groupboxOptions(new QGroupBox("Options", this)),
m_vlayoutOptions(new QVBoxLayout(m_groupboxOptions)),
m_checkboxAfficherMesures(new QCheckBox("Afficher les mesures", m_groupboxOptions)),
m_buttonRAZ(new QPushButton("Remise à zéro", m_groupboxOptions)),
m_widgetAlpha(new QWidget(m_groupboxOptions)),
m_hlayoutAlpha(new QHBoxLayout(m_widgetAlpha)),
m_labelAlphaText(new QLabel("Alpha :", m_groupboxOptions)),
m_labelAlphaValeur(new QLabel("50", m_groupboxOptions)),
m_sliderAlpha(new QSlider(m_groupboxOptions))
{
// Alpha
m_sliderAlpha->setMaximum(100);
m_sliderAlpha->setValue(50);
m_hlayoutAlpha->addWidget(m_labelAlphaText);
m_hlayoutAlpha->addWidget(m_labelAlphaValeur);
m_labelAlphaText->setAlignment(Qt::AlignRight);
m_labelAlphaValeur->setAlignment(Qt::AlignLeft);
m_sliderAlpha->setOrientation(Qt::Horizontal);
QObject::connect(m_sliderAlpha, SIGNAL(valueChanged(int)), m_labelAlphaValeur, SLOT(setNum(int)));
// Afficher Mesures
m_checkboxAfficherMesures->setChecked(true);
// Options
m_vlayoutOptions->addWidget(m_buttonRAZ);
m_vlayoutOptions->addWidget(m_checkboxAfficherMesures);
m_vlayoutOptions->addWidget(m_widgetAlpha);
m_vlayoutOptions->addWidget(m_sliderAlpha);
// Mesures
m_scrollareaMesures->setWidget(m_widgetMesures);
m_vlayoutGroupBoxMesures->addWidget(m_scrollareaMesures);
// Total
m_vlayoutTotal->addWidget(m_groupboxOptions);
m_vlayoutTotal->addWidget(m_groupboxMesures);
setLayout(m_vlayoutTotal);
}
void PanneauMesure::add(Mesure *mesure)
{
m_vlayoutMesures->addWidget(mesure);
QObject::connect(m_buttonRAZ, SIGNAL(clicked()), mesure, SLOT(close()));
QObject::connect(m_buttonRAZ, SIGNAL(clicked()), this, SLOT(remiseAZero()));
}
void PanneauMesure::remiseAZero()
{
m_sliderAlpha->setValue(50);
m_checkboxAfficherMesures->setChecked(true);
}
主文件
#include <QApplication>
#include "PanneauMesure.h"
#include "Mesure.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
PanneauMesure panneau;
Mesure mesure1("14°", &panneau);
Mesure mesure2("12°", &panneau);
Mesure mesure3("9°", &panneau);
Mesure mesure4("8°", &panneau);
Mesure mesure5("7°", &panneau);
Mesure mesure6("5°", &panneau);
Mesure mesure7("2°", &panneau);
Mesure mesure8("1°", &panneau);
panneau.add(&mesure1);
panneau.add(&mesure2);
panneau.add(&mesure3);
panneau.add(&mesure4);
panneau.add(&mesure5);
panneau.add(&mesure6);
panneau.add(&mesure7);
panneau.add(&mesure8);
panneau.show();
return app.exec();
}