How to properly align rich text (HTML) in Qt / PyQt / PySide QLabel?

I have a simple task.

I want to align richtext (HTML) in Qt or PyQt or PySide QLabel. QLabel works fine until I resize the widget, making it smaller than the length of the text. At this point, the text on the right is turned off. QLabel works correctly with clear text. Actually, this is just a simplified version of the question here .

In the PyQt example below, I list numbers from one to ten. I want to always see the number ten, even when I resize the widget. It works for plain text, but breaks into richtext (HTML). Is this a bug in Qt? I added some 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_()) 
+4
source share
1 answer

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

But you can use a stylesheet that should work with rich text:

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

All Articles