首页 > 解决方案 > 如何避免 QStackedLayout 中包含隐藏的 QWidget 高度?

问题描述

我有这种情况,我已经挣扎了几个小时:

我有一个主小部件,其布局设置为main_layout,在此之前将 3 个布局添加到main_layout

  1. 一个 QVBoxLayout (header_app_layout)
  2. 第二个 QVBoxLayout (header_loader_layout)
  3. 一个 QStackBoxLayout (content_layout)

QStackBoxLayout (content_layout) 添加了 2 个子小部件,其中仅显示了 1 个:

  1. m_content1_widget(显示的小部件)
  2. m_content2_widget

我的问题是,content_layout考虑到小部件中隐藏的子小部件m_content1_widget的高度: ,因此main_widget高度溢出。如果我content_layout从类型更改QStackedLayoutQVBoxLayout,一切都开始正常工作。所以在我看来,QStackedLayout是尊重隐藏小部件的高度,我不希望它这样做。有什么想法可以克服这个问题吗?这是代码:

主文件

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    main_widget w;
    w.setFixedSize(400, 400);
    w.show();
    return app.exec();
}

main_widget.h

#include <QWidget>
#include <QLabel>
#include <QStackedLayout>
#include <QVBoxLayout>
#include <QPushButton>

class main_widget : public QWidget
{
    Q_OBJECT

public:
    main_widget();
};
 

main_widget.cpp

#include "main_widget.h"

main_widget::main_widget()
    : QWidget(nullptr)
{
    QVBoxLayout *header_app_layout = new QVBoxLayout();
    QLabel *header_app_label = new QLabel();
    header_app_label->setText(tr("Header"));
    header_app_layout->addWidget(header_app_label);

    QVBoxLayout* header_loader_layout = new QVBoxLayout();
    QLabel* header_loader_label = new QLabel();
    header_loader_label->setText(tr("Header Loader"));
    header_loader_layout->addWidget(header_loader_label);

    QWidget *m_content1_widget = new QWidget();
    QVBoxLayout* content1_layout = new QVBoxLayout(m_content1_widget);
    QPushButton* content1_button1 = new QPushButton(tr("content1_button1"));
    content1_layout->addWidget(content1_button1);
    QPushButton* content1_button2 = new QPushButton(tr("content1_button2"));
    content1_layout->addWidget(content1_button2);
    QPushButton* content1_button3 = new QPushButton(tr("content1_button3"));
    content1_layout->addWidget(content1_button3);
    content1_button2->hide(); //Hidden for now. But it's height is being included
    content1_button3->hide();

    QWidget* m_content2_widget = new QWidget();
    QVBoxLayout* content2_layout = new QVBoxLayout(m_content2_widget);
    QPushButton* content2_button1 = new QPushButton(tr("content2_button1"));
    content2_layout->addWidget(content2_button1);
    QPushButton* content2_button2 = new QPushButton(tr("content2_button2"));
    content2_layout->addWidget(content2_button2);
    QPushButton* content2_button3 = new QPushButton(tr("content2_button3"));
    content2_layout->addWidget(content2_button3);
    content2_button2->hide();
    content2_button3->hide();


    QStackedLayout* content_layout = new QStackedLayout(); //Doesn't work
    //QVBoxLayout *content_layout = new QVBoxLayout(); //Works, but I need it to be of type `QStackedLayout` to show the 2 child widgets conditionally
    content_layout->addWidget(m_content1_widget);
    content_layout->addWidget(m_content2_widget);
    content_layout->setStackingMode(QStackedLayout::StackingMode::StackOne);
    content_layout->setCurrentIndex(0);

    QVBoxLayout* main_layout = new QVBoxLayout;
    main_layout->addLayout(header_app_layout); //Adding a QVBoxLayout
    main_layout->addLayout(header_loader_layout); //Adding another QVBoxLayout
    main_layout->addSpacing(32);
    main_layout->addLayout(content_layout); //Adding the QStackedLayout

    this->setLayout(main_layout);
}

标签: qtqt5

解决方案


我能够通过调用来解决这个问题adjustSizem_content2_widget它消除了内部隐藏控件占用的空间m_content2_widget

m_content2_widget->adjustSize();

推荐阅读