Qt widget (with layout) space. What is it? How to remove?(Qt 小部件(带布局)空间.它是什么?如何去除?)
问题描述
我编写了一个使用小部件作为容器的程序(用于其他小部件).因为容器的内容随着程序的生命周期而变化,容器小部件有一个与之关联的布局,因此它可以正确调整大小.问题是容器似乎占用了一些空间.
I have written a program that uses widgets as container (for other widgets). Because the contents of the container varies along the program life, the container widget has a layout associated to it so it resizes properly. The problem is that the container seems to consume some space.
在下面的程序中,我重现了这个问题:我有一个带有几个标签的组,其中一个包含在容器中(小部件 w -及其布局 t- 包括标签那是什么额外的空间?").我的目标是让所有标签之间的间距相同,无论它们是否在容器中.(容器不应占用空间)
In the following program, I have reproduced the problem: I have a group with a few labels, where one of them is included in a container (the widget w -and its layout t- includes the label "what is that extra space?"). My goal is to get the spacing between all labels the same, regardless whether they are in containers or not. (the container should not consume space)
我还尝试为小部件的不同部分着色.我的填充在哪里?小部件之间(蓝色之间)的额外空间是多少.我该如何删除它?
I have also tried to color the different parts of the widgets. Where is my padding? What is the extra space between the widgets (between the blue). And how do I remove it?
#include <QApplication>
#include <QtCore>
#include <QMainWindow>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <stdlib.h>
QMdiArea* g1;
QGroupBox* g1a;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow* main_window = new(QMainWindow);
main_window->resize(200, 200);
main_window->setWindowTitle("Hello");
g1a = new QGroupBox("G1A", g1);
QVBoxLayout *g1a_l = new QVBoxLayout(g1a);
g1a_l->setSpacing(0);
main_window->setCentralWidget(g1a);
g1a_l->addWidget((QLabel*)new QLabel(" Nice Label1"));
g1a_l->addWidget((QLabel*)new QLabel(" Nice Label2"));
QWidget* w=new QWidget(0);
w->setStyleSheet( "border: 2 solid blue; padding: 2 solid yellow;" );
QVBoxLayout* t=new QVBoxLayout(w);
t->setSpacing(0);
t->addWidget(new QLabel("What is that extra space??",w));
g1a_l->addWidget(w);
g1a_l->addWidget((QLabel*)new QLabel(" Nice Label3"));
g1a_l->addWidget((QLabel*)new QLabel(" Nice Label4"));
//sub_window->adjustSize();
main_window->show(); //How to I get that to recaclulate the size of its contents?
return app.exec();
}
推荐答案
这是内容边距
删除它:
t->setContentsMargins(0,0,0,0);
这篇关于Qt 小部件(带布局)空间.它是什么?如何去除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Qt 小部件(带布局)空间.它是什么?如何去除?
基础教程推荐
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07