如何右对齐 QFormLayout 中的第二列?

How can I right-align the second column in a QFormLayout?

我有一个来自 tk 的 gui,我正在尝试使用 pyqt 复制它。您可以同时看到 here。在 tk 图像中,您会注意到在 "Top 5" 区域中,客户名称用白色填充 space 并且发货数量右对齐。

我已经在 pyqt 中用 QtGui.QFormLayout 实现了这个。我已经尝试 setFormAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignRight) 但这仍然会导致您在我的 link.

中看到的图像

我做错了什么? Here 是我的全部 pyqt 代码 - 如果您对备用有任何其他建议,'better' 即更多 pythonic 方法,请告诉我。

设置您在第 43 行创建的各个 QLabel 的对齐方式:

cust = QtGui.QLabel(' - Customer - ')
qty = QtGui.QLabel('1234')
qty.setAlignment(QtCore.Qt.AlignRight)
self.topFive.append([cust, qty])
ov_custs.addRow(cust, qty)

结果是this

我还建议您使用 QVBoxLayout 作为主布局,并使用 QHBoxLayout 来包含 "Part Number" 和 QLineEdit 小部件。这样,当您从 tk 布局中添加其他小部件时,您不必担心网格坐标:

vbox = QtGui.QVBoxLayout()
overviewBox = QtGui.QGroupBox('Overview')

# Main window
part_num_layout = QtGui.QHBoxLayout()
part_num_layout.addWidget(QtGui.QLabel('Part Number'))
part_num_layout.addWidget(QtGui.QLineEdit(), stretch=1)
vbox.addLayout(part_num_layout)
vbox.addWidget(overviewBox)
...
self.setLayout(vbox)