[PyQt] How to right align rich text (HTML) in a QLabel?

Laurence Anthony antwebid at gmail.com
Sat Apr 27 13:45:53 BST 2013


I asked the following question on StackOverflow but didn't get an answer.
Perhaps people here can help.

I have a simple task.

I want to right align richtext (HTML) in a PyQt 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 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
(labelPT) but breaks for richtext (labelRT). How can I get the labelRT
version working properly?

Any help would be very welcome.

Laurence

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_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130427/d4394b8c/attachment.html>


More information about the PyQt mailing list