Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align QLabel text to label's right edge

Tags:

python

pyqt

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.

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_())
like image 756
alphanumeric Avatar asked Jul 25 '14 21:07

alphanumeric


1 Answers

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

label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
like image 151
mdurant Avatar answered Sep 23 '22 01:09

mdurant