QGroupBox中的QScrollArea

我正在尝试实现以下架构:

  • QGroupBox - > setLayout

  • QVBoxLayout - > addWidget

  • QScrollArea - > setWidget

  • QWidget - > setLayout

  • QVBoxLayout - > addWidget

  • 我的小部件(Mesures)

我正在寻找的结果是在我的所有小部件中都有一个带有scrollarea的groupbox . 但是当我尝试实现它时,在scrollarea中没有任何东西 .

这就是我所拥有的:http://imgur.com/SgQkJ

编辑:这是我删除scrollarea并直接将我的小部件放入gorupbox http://imgur.com/iuTnV时的内容

这是我的代码:

Mesure.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

Mesure.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);
}

Main.cpp的

#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();
}

回答(1)

2 years ago

默认情况下, QScrollArea 视图(内部滚动部分)在内容更改时不会调整其大小,您可以使用以下任一方法更改它:

m_vlayoutMesures->setSizeConstraint(QLayout::SetMinAndMaxSize);

要么

m_scrollareaMesures->setWidgetResizable(true);

后者还将调整 QScrollArea 的外部大小,以尽可能避免滚动条 .