This is a screenshot of a QFormLayout holding a series of widgets. Note that the top and bottom input widgets are not horizontally aligned to the middle two rows. Note also that the labels on the left are not vertically aligned to the middle two rows.
这是一个包含一系列小部件的QFormLayout的屏幕截图。注意,顶部和底部的输入小部件不是水平对齐到中间的两行。还要注意,左边的标签不是垂直对齐到中间的两行。
The difference is that the top and bottom widgets are plain QLineEdit and QTextEdit widgets, the second row is a QWidget with a QHBoxLayout holding a QSpinBox and QDateTimeEdit, and the third is a QStackedLayout containing QWidgets each with a QHBoxLayout and API-supplied widgets inside those.
不同之处在于,顶部和底部的小部件是普通的QLineEdit和QTextEdit小部件,第二行是QWidget,其中包含QSpinBox和QDateTimeEdit的QHBoxLayout,第三行是QStackedLayout,其中包含QWidgets,每个小部件都有QHBoxLayout和api提供的小部件。
It looks like these middle two have extra margins. I can (partially) improve the situation by calling QLayout::setContentsMargins on the plain QWidgets' layouts. In fact, in the image above I already have, without that it's worse.
看起来中间的两个有额外的边距。我可以(部分地)通过调用QLayout::setContentsMargins在简单的QWidgets的布局上改进情况。事实上,在上面的图片中,我已经有了,没有它,情况会更糟。
I haven't interfered with styles in the application, it's all system-default. This seems to affect Qt5 on Ubuntu 15.04, I don't think I saw this back when it was a Qt4 application.
我没有干涉应用程序中的样式,它是系统默认的。这似乎影响了ubuntu15.04上的Qt5,我不认为我在Qt4应用时看到过它。
I tried setStyleSheet ("QWidget {margin:0;}")
in the top-level widget, that introduced all sorts of problems. I also tried the variation setStyleSheet (".QWidget {margin:0;}")
but that had no effect. QLayout::setSpacing also had no effect, and setting padding:0
in the stylesheet doesn't fix it either.
我在*小部件中尝试了setStyleSheet(“QWidget {margin:0;}”),它引入了各种各样的问题。我还尝试了变体setStyleSheet(”)。但是没有效果。设置间距也没有影响,样式表中的设置填充:0也不能修复它。
Nothing I've tried seems to bring the spacing around these custom-layout QWidgets in line with the API-supplied widgets.
我尝试过的任何东西似乎都不能使这些自定义布局QWidgets与api提供的小部件保持一致。
What have I missed? Thanks.
我错过了什么?谢谢。
1 个解决方案
#1
0
What you observe is both the struggle of multi-platform ui and the simple fact that some widgets are horrible. Given a limited amount of time and a big task, dev teams will not treat fairly the set of features. Powerful widgets and layouts like standard widgets, QGridLayout
, will be maintained day and night to be defect free while legacy widgets like QStackedWidget
will be pushed at the bottom of the maintenance queue.
您看到的是多平台ui的困难和一些小部件糟糕的简单事实。给定有限的时间和艰巨的任务,开发团队不会公平地对待特性集。强大的小部件和布局(如标准小部件、QGridLayout)将被日夜维护以避免缺陷,而像QStackedWidget这样的遗留小部件将被推到维护队列的底部。
I tried to reproduce your issue using designer alone and here is what I got (Qt 4.8) :
我试着用设计师来重现你的问题(Qt 4.8):
What you can see:
你可以看到:
- The second line with a widget container has great marging
- 带有小部件容器的第二行有很大的标记。
- The third line doesn't even show the combo box (I swear it's there!!).
- 第三行甚至没有显示组合框(我发誓它在那里!!)
My two cents :
我的两个美分:
- There is a difference in behavior in the second row. Because I used
Qt4
and youQt5
, and we both set theQHBoxLayout
margin to zero then it means there is a potential regression at play, either in Qt5 or the linux implementation. So this confirms what you suspected. - 在第二行有一个不同的行为。因为我使用了Qt4和Qt5,并且我们都将QHBoxLayout边界设置为0,这意味着在Qt5或linux实现中存在潜在的回归。这证实了你的怀疑。
-
QStackedWidget
is horrible. It is the usual widget provided to give quick and basic functionality rather implementing the real deal. Here it would be a widget with aQStackedLayout
. Like I said I suspect those kind of widgets to receive less love when big development efforts are required. - QStackedWidget是可怕的。它是通常提供的用于提供快速和基本功能的小部件,而不是实现真正的事务。这里是一个带有QStackedLayout的小部件。就像我说的,我怀疑当需要进行大的开发时,这些小部件会受到较少的喜爱。
- In these situations, use
QGridLayout
instead of aQStackedLayout
of widgets with layout of widget. It will solve the issue of labels not being aligned. Difficult to set up at first but the result is usually neat. - 在这些情况下,使用QGridLayout而不是包含widget布局的QStackedLayout。它将解决标签未对齐的问题。一开始很难设置,但结果通常是整洁的。
By the way, I switched to a widget with a stackedlayout and here is the result :
顺便说一下,我切换到一个包含断式布局的小部件,结果如下:
#1
0
What you observe is both the struggle of multi-platform ui and the simple fact that some widgets are horrible. Given a limited amount of time and a big task, dev teams will not treat fairly the set of features. Powerful widgets and layouts like standard widgets, QGridLayout
, will be maintained day and night to be defect free while legacy widgets like QStackedWidget
will be pushed at the bottom of the maintenance queue.
您看到的是多平台ui的困难和一些小部件糟糕的简单事实。给定有限的时间和艰巨的任务,开发团队不会公平地对待特性集。强大的小部件和布局(如标准小部件、QGridLayout)将被日夜维护以避免缺陷,而像QStackedWidget这样的遗留小部件将被推到维护队列的底部。
I tried to reproduce your issue using designer alone and here is what I got (Qt 4.8) :
我试着用设计师来重现你的问题(Qt 4.8):
What you can see:
你可以看到:
- The second line with a widget container has great marging
- 带有小部件容器的第二行有很大的标记。
- The third line doesn't even show the combo box (I swear it's there!!).
- 第三行甚至没有显示组合框(我发誓它在那里!!)
My two cents :
我的两个美分:
- There is a difference in behavior in the second row. Because I used
Qt4
and youQt5
, and we both set theQHBoxLayout
margin to zero then it means there is a potential regression at play, either in Qt5 or the linux implementation. So this confirms what you suspected. - 在第二行有一个不同的行为。因为我使用了Qt4和Qt5,并且我们都将QHBoxLayout边界设置为0,这意味着在Qt5或linux实现中存在潜在的回归。这证实了你的怀疑。
-
QStackedWidget
is horrible. It is the usual widget provided to give quick and basic functionality rather implementing the real deal. Here it would be a widget with aQStackedLayout
. Like I said I suspect those kind of widgets to receive less love when big development efforts are required. - QStackedWidget是可怕的。它是通常提供的用于提供快速和基本功能的小部件,而不是实现真正的事务。这里是一个带有QStackedLayout的小部件。就像我说的,我怀疑当需要进行大的开发时,这些小部件会受到较少的喜爱。
- In these situations, use
QGridLayout
instead of aQStackedLayout
of widgets with layout of widget. It will solve the issue of labels not being aligned. Difficult to set up at first but the result is usually neat. - 在这些情况下,使用QGridLayout而不是包含widget布局的QStackedLayout。它将解决标签未对齐的问题。一开始很难设置,但结果通常是整洁的。
By the way, I switched to a widget with a stackedlayout and here is the result :
顺便说一下,我切换到一个包含断式布局的小部件,结果如下: