I have a QtQuick Label
in a QML file like so:
import QtQuick.Controls 1.3 as Controls
Controls.Label {
id: lbl
text: "This is some <b>bold text</b> with extra white space"
}
If the text
property of my label contains any HTML, then the label renders it as HTML and the multiple spaces in the original text are compressed down to a single space (if the text contains no HTML then it is rendered as normal text and the spaces are preserved).
QWidget
has a setStyleSheet
method that apparently supports the style "white-space: pre-wrap" which is what I need to get the HTML rendering to preserve the whitespace, but I'm not sure if I can apply this to a Label in a QML file. Is there any way to achieve this?
Edit: This answer: https://stackoverflow.com/a/2756376/14606 shows setting the styleSheet property for a QLabel
. Is there any way to write a function that will let me pass my QtQuick Label
and cast it as a QLabel
and set the stylesheet in this way?
You can actually achieve what I was looking for (a Label
that can display HTML while preserving white space) by doing a text replace on your original string and replacing all regular spaces (" ") with ​ 
(which is a zero-length string followed by a regular string). The Label
will then faithfully render consecutive spaces, as if the white-space style had been set to pre-wrap. Unfortunately this only kind of works (for my purposes at least) since the Label
does not handle line wrapping the same way in both cases.