5

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_())
Community
  • 1
  • 1
LozzerJP
  • 856
  • 1
  • 8
  • 23

1 Answers1

3

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;")
Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37
  • I just tried it with PySide 1.1.1 but it didn't work. I also tried your style sheet suggestion with PyQt and Pyside. It doesn't work either. Are your sure the align works for you *when you shrink the widget to a size smaller than the text*? For me, the richtext (html) widget starts cutting to the right. – LozzerJP Apr 22 '13 at 17:04
  • @Lozzer maybe you should try it on another computer – Dan Barzilay May 04 '13 at 10:50
  • @DanBarzilay, I just did a completely fresh install of the latest Python (2.7.4) and PyQt (4.10.1) on a completely clean os. The effect is exactly the same as I described above. When you resize the labels to be smaller than the text width, the right side of the Richtext label gets cut, whereas the left side gets cut for a non-Richtext label. Does the above code work correctly for you? – LozzerJP May 07 '13 at 12:42
  • @Lozzer I have the problem as well, I'm 95% sure it's a PyQt bug. – Dan Barzilay May 07 '13 at 15:59
  • @DanBarzilay. In a weird way that's good to know. I was worried that it was just me. I will put in a bug report. But, actually, I think the bug might even be in Qt (not PyQt). – LozzerJP May 08 '13 at 14:58
  • @Lozzer how did you come up with that? – Dan Barzilay May 09 '13 at 19:12