[英]Qt remove empty space between widgets on QVBoxLayout
I have annoying problem.我有烦人的问题。 I created QVBoxLayout on which I added my widgets.
我创建了 QVBoxLayout 并在其上添加了我的小部件。 This is sample from my constructor:
这是来自我的构造函数的示例:
layout = new QVBoxLayout;
layout->setMargin(0);
layout->setContentsMargins(QMargins(0,0,0,0));
layout->setSpacing(0);
And then I have function to add widgets.然后我有添加小部件的功能。
layout->addWidget(_wave);
_wave is my own widget. _wave 是我自己的小部件。 But you can add whatever you want, for example QButton.
但是您可以添加任何您想要的内容,例如 QButton。
What do I want achieve?我想要实现什么? Similar like this but without any spaces beetween widgets added to layout.
与此类似,但在布局中添加的小部件之间没有任何空格。 Just only QButtons or other widget, sticked each other.
只是 QButtons 或其他小部件,相互粘在一起。
I added everywhere setMargins, setSpacing etc. Please help me with that, I don't really have an idea what should I do.我到处都添加了 setMargins、setSpacing 等。请帮我解决这个问题,我真的不知道我该怎么做。
Sorry for colors, but I wanted to mentioned what I want to achieve.对不起颜色,但我想提到我想要实现的目标。 I have mainWindow on which I added QWidget.
我有在其上添加 QWidget 的 mainWindow。 This widget have blue background.
这个小部件有蓝色背景。 Then to the layout, Im addding some widgets, which are orange on this image.
然后到布局,我添加了一些小部件,在这张图片上是橙色的。 I just want to be sure, that this blue background between widget isnt visible.
我只想确定,小部件之间的蓝色背景不可见。 I want to have widget under widget, without any space.
我想在小部件下有小部件,没有任何空间。
I know the question was posted a year ago, but if it can help some people save some time, I would be glad.我知道这个问题是一年前发布的,但如果它可以帮助某些人节省一些时间,我会很高兴。 I had the same problem and was able to solve it by setting:
我遇到了同样的问题,并且能够通过设置来解决它:
QFrame.setFrameShape()
to NoFrame
QFrame.setFrameShape()
到NoFrame
QFrame.setLineWidth()
to 0. QFrame.setLineWidth()
为 0。 The image below is the result of 2 QtextEdit
put side by side in a Layout, using the described method.下图是使用所述方法将 2 个
QtextEdit
并排放置在布局中的结果。
http://doc.qt.io/qt-5/qframe.html#lineWidth-prop http://doc.qt.io/qt-5/qframe.html#lineWidth-prop
Try this to achieve a "tight" look.试试这个以获得“紧身”的外观。 Note that if you resize the parent widget, they will not move.
请注意,如果您调整父小部件的大小,它们将不会移动。 Not sure if this is what you want or not, but...
不确定这是否是您想要的,但是...
// After all widgets are added to the layout
layout->insertStretch( -1, 1 );
The 2nd argument needs to be higher than any other stretch factor.第二个参数需要高于任何其他拉伸因子。 Stretch factor is default zero, so if you don't set it, the above one-liner should work.
拉伸因子默认为零,所以如果你不设置它,上面的单线应该可以工作。
As the Qt document says, some default GUI widget has extra frame around it.正如 Qt 文档所说,一些默认的 GUI 小部件周围有额外的框架。
Take QPushButton
as an example:以
QPushButton
为例:
In some GUI styles a default button is drawn with an extra frame around it , up to 3 pixels or more .
在某些 GUI 样式中,默认按钮周围有一个额外的框架,最多 3 像素或更多。 Qt automatically keeps this space free around auto-default buttons, ie auto-default buttons may have a slightly larger size hint.
Qt 会自动保留自动默认按钮周围的空间,即自动默认按钮可能具有稍大的尺寸提示。
So the culprit is not the layout but the widget itself instead.所以罪魁祸首不是布局,而是小部件本身。 "Invisible" margin of a default widget remains even if the spacing of the layout has been set to 0.
即使布局的间距已设置为 0,默认小部件的“不可见”边距也会保留。
If you really need a " compact " layout, my suggestion is to resort to the stylesheet of the widget.如果你真的需要一个“紧凑”的布局,我的建议是使用小部件的样式表。
Here are examples to illustrate the idea:以下是说明该想法的示例:
Here, we have a vertical layout with both margin and spacing equal to 0, and the buttons with custom stylesheet:在这里,我们有一个边距和间距都等于 0 的垂直布局,以及带有自定义样式表的按钮:
QPushButton {
border: 2px solid #8f8f91;
border-radius: 6px;
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #f6f7fa, stop: 1 #dadbde);
min-width: 80px;
}
// (further stylesheet for button click is needed)
Resetting stylesheet allows us to make the layout more compact:重置样式表允许我们使布局更紧凑:
Insert a default QPushButton
to see the difference:插入一个默认的
QPushButton
来看看区别:
You can see there is space around the default QPushButton
.您可以看到默认
QPushButton
周围有空间。 That's the same for other kinds of widgets.其他类型的小部件也是如此。
I could be wrong but there does not seem to be an obvious and non-hack of a way to do what you desire.我可能是错的,但似乎没有一种明显且非黑客的方法来做你想做的事。 However, according to the documentation you may find that just doing a manual layout will solve your problem.
但是,根据文档,您可能会发现仅进行手动布局即可解决您的问题。 ( http://doc.qt.io/qt-5/layout.html : Manual Layout)
( http://doc.qt.io/qt-5/layout.html :手动布局)
It seems as if one could implement a Layout class that actually provides a very compact horizontal or vertical layout and I believe I had written one a few years ago using Python and Qt.似乎可以实现一个 Layout 类,它实际上提供了一个非常紧凑的水平或垂直布局,我相信我几年前使用 Python 和 Qt 编写了一个。 With that in mind, you should be able to implement your own
MyCompactLayout(horizontal=True)
and use it.考虑到这一点,您应该能够实现自己的
MyCompactLayout(horizontal=True)
并使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.