#include "nmGUIDrawerWidget.h" nmGUIDrawerWidget::nmGUIDrawerWidget(const QString &sTitle, QWidget *parent) : QWidget(parent), m_bExpanded(true), m_sTitle(sTitle) { initUI(); this->setStyleSheet(""); } void nmGUIDrawerWidget::initUI() { // 创建标题 QWidget *headerWidget = new QWidget(this); headerWidget->setObjectName("headerWidget"); m_pHeaderLayout = new QHBoxLayout(headerWidget); m_pHeaderLayout->setContentsMargins(2, 2, 2, 2); m_pToggleButton = new QToolButton(this); m_pToggleButton->setArrowType(Qt::DownArrow); m_pToggleButton->setStyleSheet("QToolButton { background: transparent; border: none; }"); // 设置按钮透明背景 m_pTitleLabel = new QLabel(m_sTitle, this); m_pTitleLabel->setStyleSheet("font-weight: bold; background: transparent;"); // 设置标题透明背景 m_pHeaderLayout->addWidget(m_pToggleButton); m_pHeaderLayout->addWidget(m_pTitleLabel); m_pHeaderLayout->addStretch(); // 创建内容 m_pWidgetContent = new QWidget(this); m_pWidgetContent->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); m_pWidgetContent->setVisible(true); // 默认展开 m_pWidgetContent->setObjectName("contentWidget"); // 设置objectName以便样式表定位 m_pWidgetContent->setAutoFillBackground(true); // 确保内容区白底稳定 // 创建布局 QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->setSpacing(0); mainLayout->setContentsMargins(0, 0, 0, 0); mainLayout->addWidget(headerWidget); mainLayout->addWidget(m_pWidgetContent); connect(m_pToggleButton, SIGNAL(clicked()), this, SLOT(toggleExpand())); } void nmGUIDrawerWidget::toggleExpand() { setExpanded(!m_bExpanded); } void nmGUIDrawerWidget::setExpanded(bool expanded) { if (expanded != m_bExpanded) { m_bExpanded = expanded; m_pToggleButton->setArrowType(expanded ? Qt::DownArrow : Qt::RightArrow); m_pWidgetContent->setVisible(expanded); emit expansionChanged(expanded); } } void nmGUIDrawerWidget::setContentLayout(QLayout *layout) { m_pWidgetContent->setLayout(layout); } void nmGUIDrawerWidget::setStyleSheet(const QString &style) { QString customStyle = "#headerWidget {" " background-color: rgb(173, 216, 230);" // 设置标题栏背景颜色为浅蓝色 " width: 100%;" "}" "#contentWidget {" " background-color: white;" // 设置内容区背景颜色为白色 "}"; QWidget::setStyleSheet(customStyle); // 调用父类方法 }