4
votes

I have a simple task.

I want to right align richtext (HTML) in a Qt or PyQt or PySide QLabel. The QLabel works fine until I resize the widget making it smaller than the text length. At that point, the text to the right gets cut off. The QLabel works properly with plain text. In fact, this is just a simplified version of the question here.

In the PyQt example below, I list numbers one to ten. I want to always see the number 'ten' even when I resize the widget. It works for plain text but breaks for richtext (HTML). Is this a bug in Qt? I've added a couple of screenshots to show the effect.

Before shrinking the widget

After shrinking the widget

from PyQt4 import QtGui, QtCore

import sys

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    mw = QtGui.QWidget()

    labelPT = QtGui.QLabel()
    labelPT.setText('one two three four five six seven eight nine ten')
    labelPT.setAlignment(QtCore.Qt.AlignRight)

    labelRT = QtGui.QLabel()
    labelRT.setText('one two three four <b>five</b> six seven eight nine ten')
    labelRT.setAlignment(QtCore.Qt.AlignRight)

    vbox = QtGui.QVBoxLayout()
    vbox.addWidget(labelPT)
    vbox.addWidget(labelRT)

    mw.setLayout(vbox)
    mw.setMinimumWidth(30)
    mw.show()

    sys.exit(app.exec_())
1

1 Answers

3
votes

Hum... It works fine with PySide (version 1.1.2 on Windows).

But, you can use style sheet that should work with a rich text:

labelRT.setStyleSheet("qproperty-alignment: AlignRight;")