如何删除 Qt 小部件(带布局)空间

发布于 2025-01-01 23:22:07 字数 1768 浏览 0 评论 0原文

我编写了一个使用小部件作为容器(用于其他小部件)的程序。由于容器的内容在程序生命周期中会发生变化,因此容器小部件具有与其关联的布局,因此可以正确调整大小。

问题是容器似乎消耗了一些空间。

在下面的程序中,我重现了这个问题:

我有一个包含几个标签的组,其中一个标签包含在一个容器中(小部件 w - 及其布局 t- 包含标签“那个额外的空格是什么?”)。

我的目标是使所有标签之间的间距相同,无论它们是否在容器中(容器不应占用空间)。

我还尝试为小部件的不同部分着色。

  • 我的填充物在哪里?
  • 小部件之间(蓝色之间)的额外空间是多少?
  • 我该如何删除它?
#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();
}

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.

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)?
  • 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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍风燃霜 2025-01-08 23:22:07

这是contentsMargins

要删除它,请使用:

t->setContentsMargins(0,0,0,0);

This is contentsMargins.

To remove it, use:

t->setContentsMargins(0,0,0,0);
以可爱出名 2025-01-08 23:22:07

您所指的空间是内容边距。 此处的文档描述了小部件的使用方式绘制:

在此处输入图像描述

要消除多余的空间,您必须调用:

widget->setContentsMargins(0, 0, 0, 0);

我相信最终结果应该如下所示像这样:

在此处输入图像描述

The space you're referring to is a content margin. The docs here describe how the widgets are drawn:

enter image description here

To get rid of that extra space, you have to call:

widget->setContentsMargins(0, 0, 0, 0);

I believe the end result should look like this:

enter image description here

梦毁影碎の 2025-01-08 23:22:07

我记得控制间距的方法是在小部件之间添加专用的 QSpacerItem 项。

但我首先尝试弄清楚 w 在做什么。为什么不直接调用g1a_l->addLayout(t);呢?

The method I recall to control spacing is to add dedicated QSpacerItem items between the widgets.

But I'm first trying to figure out what w is doing. Why not call g1a_l->addLayout(t); directly?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文