Search code examples
pythonpyqt4qtgui

New Line Character and PyQt


Using QtGui.QMessageBox to display the messages, warnings and errors.

It seems that QMessageBox doesn't want to work with "\n" new line character when used with html tags

message =  "<a href = http://www.google.com> GOOGLE</a> This a line number one.\n This a line number two. \n And this is a line number three."

is all being displayed as one long line when displayed within QMessageBox.

Thanks in advance!


Solution

  • The behaviour you are seeing is entirely as expected. It is part of the HTML 4 spec that, other than inside PRE tags, sequences of whitepsace characters should always be collapsed to a single space. To quote the relevant part of the spec:

    Note that a sequence of white spaces between words in the source document may result in an entirely different rendered inter-word spacing (except in the case of the PRE element). In particular, user agents should collapse input white space sequences when producing output inter-word space.

    So, when you need to insert line-breaks, do it explicitly using the <br> tag.

    PS:

    It's also worth noting here that Qt's text widgets only support a limited set of HTML tags, attributes and CSS properties. For full details, see the Supported HTML Subset in the Qt docs.