PyQt4 - QLayout 间距不明确

发布于 2024-12-22 05:03:14 字数 2214 浏览 0 评论 0原文

我有一个 QHBoxLayout,我将小部件一一添加到其中。
我交替添加自定义小部件,而不是 QLabel(重复)。
QHBoxLayout 由 QGroupBox 拥有。

但是,我注意到当布局中添加的小部件很少时,自定义小部件和 QLabel 之间的间距是“不规则”的。

我打算让 QLabels 在自定义小部件之间的间隙中居中,但随着添加更多小部件,QLabels 才会接近中心。

这些屏幕截图显示了两种情况。
所需的情况(仅在布局中有大量小部件时出现),
以及不需要的情况(当布局中有小部件时发生)。

底部情况不规则间距,顶部良好。

正如您在底部情况中看到的,QLabel 未居中自定义小部件之间。相反,它们离右边很远!

是什么导致了这种行为?

我相信 QGroupBox 具有居中(水平)对齐方式,并且 QLabels 的固定宽度为 10(或“->”QLabel 为 20)像素(以避免难看的重叠)。

任何帮助将不胜感激!
谢谢!

规格:
蟒蛇2.7.1
PyQt4
Windows 7

QHBoxLayout 的实例化绝对正常并且与所有示例相似。
这是填充布局的代码。

for i in range (0,len(Reactants)):

    self.WidgetHouse.Reaction_Element_Layout.addWidget(eval('self.OverallContainer_Reactants.Reactant_'+str(i)))

    # self.WidgetHouse.Reaction_Element_Layout           is the QHBoxLayout
    # self.OverallContainer_Reactants.Reactant_'+str(i)       is a Custom Widget

    if i != (len(Reactants)-1):
        tmp = QtGui.QLabel('+')
        tmp.setFixedWidth(10)
        tmp.setAlignment(QtCore.Qt.AlignCenter)
        self.WidgetHouse.Reaction_Element_Layout.addWidget(tmp)

    else:
        tmp = QtGui.QLabel('->')
        tmp.setFixedWidth(20)
        tmp.setAlignment(QtCore.Qt.AlignCenter)
        self.WidgetHouse.Reaction_Element_Layout.addWidget(tmp)

编辑:

设置 QLabels 的固定宽度 (tmp.setFixedWidth(10)) 是“正确锚定”的来源。
但是,不设置固定宽度会导致布局中的同一空间专用于 QLabels 和自定义小部件,从而导致 QLabels 和自定义小部件重叠。

固定宽度标签与布局调制宽度

与上面的代码相同,不包括 'tmp.setFixedWidt(10)'

我能做什么防止这种情况发生并不完全可怕?
我可以将标签从前面移到“后面”吗?
(在将所有内容添加到布局后,在所有自定义小部件上调用 .raise_(​​) 不起作用)

(或者我是否必须执行一些操作,例如根据布局中小部件的数量手动计算标签的适当宽度?呸呸!)

另一个编辑:

进展:

我没有更改 QLabels 的最大/最小(或固定)宽度,但我确实将它们的对齐方式设置为居中。
相反,我设置了自定义小部件的最小宽度。
这修复了明显的“重叠”(事实并非如此)并使标签看起来“更居中”。

然而,正如您所看到的,QLabels 仍然没有完全居中 - 太右了。
如果我没有在 QLabels 上设置中心对齐,它们就会太靠左。

现在可能是什么问题?

重叠已解决,锚定仍然是一个问题

(我没有在标签上设置最大宽度)

感谢迄今为止的所有帮助伙计们!

I have a QHBoxLayout which I add widgets to, one by one.
I alternate by adding a Custom Widget, than a QLabel (repeating).
The QHBoxLayout is owned by a QGroupBox.

However, I notice that the spacing between the Custom Widget and the QLabel is 'irregular' when there are few widgets added to the Layout.

I intend for the QLabels to be centered in the gap between the custom widgets, but the QLabels only approach the centre as more widgets are added.

These screenshots show the two cases.
The desired case (only occuring with lots of widgets in the layout),
and the undesired case (occuring when little widgets are in the layout).

Irregular Spacing in the bottom case, fine in the top.

As you can see in the bottom case, the QLabels are not centered between the Custom Widgets. Instead, they are very far to the right!

What is causing this behaviour??

I believe the QGroupBox has a centered (horizontal) alignment and the QLabels have a fixed width of 10 (or 20 for the '->' QLabel) pixels (to avoid ugly overlap).

Any help at all would be greatly appreciated!
Thanks!

Specs:
python 2.7.1
PyQt4
Windows 7

The instantiation of the QHBoxLayout is absolutely normal and similiar to all examples.
Here is the code for the filling of the layout.

for i in range (0,len(Reactants)):

    self.WidgetHouse.Reaction_Element_Layout.addWidget(eval('self.OverallContainer_Reactants.Reactant_'+str(i)))

    # self.WidgetHouse.Reaction_Element_Layout           is the QHBoxLayout
    # self.OverallContainer_Reactants.Reactant_'+str(i)       is a Custom Widget

    if i != (len(Reactants)-1):
        tmp = QtGui.QLabel('+')
        tmp.setFixedWidth(10)
        tmp.setAlignment(QtCore.Qt.AlignCenter)
        self.WidgetHouse.Reaction_Element_Layout.addWidget(tmp)

    else:
        tmp = QtGui.QLabel('->')
        tmp.setFixedWidth(20)
        tmp.setAlignment(QtCore.Qt.AlignCenter)
        self.WidgetHouse.Reaction_Element_Layout.addWidget(tmp)

EDIT:

Setting a fixed width of the QLabels (tmp.setFixedWidth(10)) is the source of the 'right anchoring'.
However, not setting a fixed width results in the same space being dedicated to QLabels and Custom Widgets in the layout, leading to an overlap of QLabels and Custom Widgets.

Fixed Width Labels vs Layout modulated widths

Identical to the code above, discluding 'tmp.setFixedWidt(10)'

What can I do to prevent this that's not completely horrible?
Can I move the Labels 'back' from the front?
(Calling .raise_() on all the custom widgets after everything was added to the layout did not work)

(Or will I have to do something like manually calculate the appropriate width of the labels based off the amount of widgets in the layout? Yuck!)

ANOTHER EDIT:

Progress:

I do not change the maximum/minimum (or fixed) width of the QLabels, but I do set their alignment to center.
Instead, I set a minimum width of the custom widget.
This fixes the apparent 'overlapping' (which wasn't really the case) and makes the Labels appear 'more centered'.

However, as you can see, the QLabels still aren't perfectly centered - too far right.
If I don't set a center alignment on the QLabels they are too far left.

What could be the problem now??

Overlapping solved, anchoring still an issue

(I do not set a maximum width on the labels)

Thanks for all the help so far guys!

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

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

发布评论

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

评论(2

迷迭香的记忆 2024-12-29 05:03:14

这是一个简单的示例脚本,它是问题中 UI 的合理近似,但没有任何布局问题:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.groupBox = QtGui.QGroupBox(self)
        hbox = QtGui.QHBoxLayout(self.groupBox)
        length = 3
        for index in range(length):
            hbox.addWidget(Widget(u'H\u2082O', self))
            if index < length - 1:
                hbox.addWidget(Label(u'+', self))
            else:
                hbox.addWidget(Label(u'\u2192', self))
        hbox.addWidget(Widget(u'4 H\u2082O', self))
        hbox.addWidget(Label(u'+', self))
        hbox.addWidget(Widget(u'H\u2084O\u2082', self))
        vbox = QtGui.QVBoxLayout(self)
        vbox.addWidget(self.groupBox)
        vbox.addStretch()

class Label(QtGui.QLabel):
    def __init__(self, label, parent=None):
        QtGui.QLabel.__init__(self, label, parent)
        self.setAlignment(QtCore.Qt.AlignCenter)

class Widget(QtGui.QWidget):
    def __init__(self, label, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setMaximumWidth(100)
        layout = QtGui.QGridLayout(self)
        self.label = QtGui.QLabel(label, self)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        layout.addWidget(self.label, 0, 0, 1, 2)
        self.lineEdit = QtGui.QLineEdit(self)
        layout.addWidget(self.lineEdit, 1, 0, 1, 2)
        self.toolButton = QtGui.QToolButton(self)
        layout.addWidget(self.toolButton, 2, 0, 1, 1)
        self.comboBox = QtGui.QComboBox(self)
        layout.addWidget(self.comboBox, 2, 1, 1, 1)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

Here is a simple example script which is a reasonable approximation of the UI in the question, but without any of the layout issues:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.groupBox = QtGui.QGroupBox(self)
        hbox = QtGui.QHBoxLayout(self.groupBox)
        length = 3
        for index in range(length):
            hbox.addWidget(Widget(u'H\u2082O', self))
            if index < length - 1:
                hbox.addWidget(Label(u'+', self))
            else:
                hbox.addWidget(Label(u'\u2192', self))
        hbox.addWidget(Widget(u'4 H\u2082O', self))
        hbox.addWidget(Label(u'+', self))
        hbox.addWidget(Widget(u'H\u2084O\u2082', self))
        vbox = QtGui.QVBoxLayout(self)
        vbox.addWidget(self.groupBox)
        vbox.addStretch()

class Label(QtGui.QLabel):
    def __init__(self, label, parent=None):
        QtGui.QLabel.__init__(self, label, parent)
        self.setAlignment(QtCore.Qt.AlignCenter)

class Widget(QtGui.QWidget):
    def __init__(self, label, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setMaximumWidth(100)
        layout = QtGui.QGridLayout(self)
        self.label = QtGui.QLabel(label, self)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        layout.addWidget(self.label, 0, 0, 1, 2)
        self.lineEdit = QtGui.QLineEdit(self)
        layout.addWidget(self.lineEdit, 1, 0, 1, 2)
        self.toolButton = QtGui.QToolButton(self)
        layout.addWidget(self.toolButton, 2, 0, 1, 1)
        self.comboBox = QtGui.QComboBox(self)
        layout.addWidget(self.comboBox, 2, 1, 1, 1)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
ぃ双果 2024-12-29 05:03:14

所有问题的解决方案:

为自定义小部件指定固定宽度。

产生完美居中的小部件,而不是“重叠”小部件。
:)

感谢大家的帮助!

Solution to all problems:

Give the custom widgets a fixed width.

Results in perfectly centered, not 'overlapping' widgets.
:)

Thanks for all the help guys!

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