qt - 如何避免 QStackedLayout 中包含隐藏的 QWidget 高度?
问题描述
我有这种情况,我已经挣扎了几个小时:
我有一个主小部件,其布局设置为main_layout
,在此之前将 3 个布局添加到main_layout
:
- 一个 QVBoxLayout (header_app_layout)
- 第二个 QVBoxLayout (header_loader_layout)
- 一个 QStackBoxLayout (content_layout)
QStackBoxLayout (content_layout) 添加了 2 个子小部件,其中仅显示了 1 个:
- m_content1_widget(显示的小部件)
- m_content2_widget
我的问题是,content_layout
考虑到小部件中隐藏的子小部件m_content1_widget
的高度: ,因此main_widget
高度溢出。如果我content_layout
从类型更改QStackedLayout
为QVBoxLayout
,一切都开始正常工作。所以在我看来,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);
}
解决方案
我能够通过调用来解决这个问题adjustSize
,m_content2_widget
它消除了内部隐藏控件占用的空间m_content2_widget
。
m_content2_widget->adjustSize();
推荐阅读
- java - spring boot:如何从应用程序属性配置数据源
- google-apps-script - 根据多个答案转到下一页 - 谷歌表格
- ruby-on-rails - 如何为具有基本身份验证的 Rails 应用程序编写集成 minitest 代码?
- mysql - Mysql根据特定列删除相似的行,除了id最高的行
- macos - Swift - 从另一个类调用 reloadData() 失败(找到 nil),但从 self 类调用成功
- socket.io - 无法在 socket.on 之外获取数据
- javascript - 如何使用键盘在 svg 路径中移动对象
- android - 创建着色器时,Android OpenGL 应用程序崩溃
- php - 用于 php 的 docker 映像无法在 Windows 10 的浏览器上运行
- json - Angular 6:从 JSON 响应后端获取哈希表数据