1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include "collapsing_widget.h"
#include <QtWidgets/qlabel.h>
#include <QtWidgets/qtoolbutton.h>
creator::collapsing_widget::collapsing_widget(QWidget *parent, const QString &text, QLayout& contentLayout)
: QWidget(parent)
{
//Create a vertcal layout
QVBoxLayout *layout = new QVBoxLayout(this);
//Add a button and a label to a horizontal layout and add it to the vertical layout
QHBoxLayout *hlayout = new QHBoxLayout();
layout->addLayout(hlayout);
QToolButton *button = new QToolButton();
button->setCheckable(true);
button->setArrowType(Qt::ArrowType::DownArrow);
//make sure the button starts checked
button->setChecked(true);
hlayout->addWidget(button);
QLabel *label = new QLabel();
label->setText(text);
hlayout->addWidget(label);
//Add the content layout to a new scrollarea and add it to the vertical layout
QScrollArea *scrollArea = new QScrollArea();
scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
scrollArea->setWidgetResizable(true);
scrollArea->setLayout(&contentLayout);
contentLayout.setContentsMargins(0, 0, 0, 0);
contentLayout.setSpacing(0);
scrollArea->setMaximumHeight(scrollArea->layout()->sizeHint().height());
scrollArea->setMinimumHeight(scrollArea->layout()->sizeHint().height());
layout->addWidget(scrollArea);
//When the button is pressed, toggle the content
connect(button, &QToolButton::toggled, [=](){
button->setArrowType(checked ? Qt::ArrowType::DownArrow : Qt::ArrowType::RightArrow);
scrollArea != nullptr && checked ? showContent(scrollArea) : hideContent(scrollArea);
});
}
void creator::collapsing_widget::hideContent(QScrollArea* scrollArea) {
scrollArea->setMaximumHeight(0);
scrollArea->setMinimumHeight(0);
this->adjustSize();
this->parentWidget()->adjustSize();
checked = true;
}
void creator::collapsing_widget::showContent(QScrollArea* scrollArea) {
scrollArea->setMaximumHeight(scrollArea->layout()->sizeHint().height());
scrollArea->setMinimumHeight(scrollArea->layout()->sizeHint().height());
this->adjustSize();
this->parentWidget()->adjustSize();
checked = false;
}
|