Python 如何将 QLabel 文本与标签的右边缘对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24965060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:32:26  来源:igfitidea点击:

How to align QLabel text to label's right edge

pythonpyqt

提问by alphanumeric

The label is given a fixed width via label.setFixedWidth(200). The text inside of label is shorter then label's width. As it is now the label text is being centered within a label. But I would like the text to be aligned with the label's right side so the text right side is edge by edge to lineEdit widget left edge.

标签通过 获得固定宽度label.setFixedWidth(200)。标签内的文本比标签的宽度短。现在标签文本在标签内居中。但我希望文本与标签的右侧对齐,因此文本右侧是一个边一个边到 lineEdit 小部件左边缘。

enter image description here

在此处输入图片说明

from PyQt4 import QtCore, QtGui

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

        self.resize(720, 480)
        cWidget = QtGui.QWidget(self)
        self.setCentralWidget(cWidget)

        layout = QtGui.QHBoxLayout(cWidget)

        label = QtGui.QLabel("     Label Text Value: ")
        label.setFixedWidth(200)
        layout.addWidget(label)

        textEdit = QtGui.QTextEdit()
        textEdit.setMaximumHeight(14)
        layout.addWidget(textEdit)

        button=QtGui.QPushButton("Browse")
        layout.addWidget(button)


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

采纳答案by mdurant

If you don't want to use spacers, this does this trick:

如果您不想使用垫片,则可以使用以下技巧:

label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)