Qt QFormLayout将标签对齐到左侧并将值对齐到右侧[重复]

问题描述 投票:0回答:2

这个问题在这里已有答案:

我有以下QFormLayout,其中包含短值行和长值行

layout = QFormLayout()
layout.setLabelAlignment(Qt.AlignLeft)
layout.setFormAlignment(Qt.AlignLeft)

layout.addRow(QLabel('Label short'), QLabel('2'))
layout.addRow(QLabel('Label long'), QLabel('1234567890'))

我得到的是:

Label short    2
Label long     1234567890

我想要的是:

Label short             2
Label long     1234567890

我将第一列称为标签列,第二列称为值列。

  • 使用setFormAlignment(),我可以将整个表单向左或向右移动,但值列对齐保持不变
  • 使用setLabelAlignment(),我可以更改标签列,但不能更改值列
  • 使用setAlignment()似乎没有任何影响

是否有控制第二列对齐的端点?

python qt pyqt
2个回答
0
投票

使用QLabel,您应该能够使用AlignRight将水平文本对齐设置为右侧

像(使用C ++,但你在Python中有相同的选项)

label->setAlignment(Qt::AlignBottom | Qt::AlignRight);


0
投票

试试吧:

from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)

        layout = QFormLayout(centralWidget)
        layout.setLabelAlignment(Qt.AlignLeft)
        layout.setFormAlignment(Qt.AlignLeft)

        layout.addRow(QLabel('Label short'), QLabel('2',          alignment=Qt.AlignRight))
        layout.addRow(QLabel('Label long'),  QLabel('1234567890', alignment=Qt.AlignRight))

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.