这个问题在这里已有答案:
我有以下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()
似乎没有任何影响是否有控制第二列对齐的端点?
使用QLabel,您应该能够使用AlignRight将水平文本对齐设置为右侧
像(使用C ++,但你在Python中有相同的选项)
label->setAlignment(Qt::AlignBottom | Qt::AlignRight);
试试吧:
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_())