Qt World Summit: Register Today!

How to manually wrap a plain QStaticText?

  • Hi,

    I am using QStaticText quite a lot, both for single line and multi-line texts. Only plain text is required, so I stick with that.

    One question remains a mystery to me: How do I manually, explicitly wrap a plain text at a certain position within the text?

    E.g. when I provide the following text:

    The text should break\nright here

    I would like it to output:

    The text should break
    right here

    How do I achieve this?

  • It seems that you can format your text as HTML code fragment and force a break where you want:

    bq. For extra convenience, it is possible to apply formatting to the text using the HTML subset supported by QTextDocument. QStaticText will attempt to guess the format of the input text using Qt::mightBeRichText(), and interpret it as rich text if this function returns true. To force QStaticText to display its contents as either plain text or rich text, use the function QStaticText::setTextFormat() and pass in, respectively, Qt::PlainText and Qt::RichText.

  • For everything else BUT line breaks, I want plain text.

    How would I ensure that the string (which comes from an XML file) is plain text?

    BTW, this is running on epically slow hardware. Will using HTML not completely kill my performance?

  • [quote author="Asperamanca" date="1322473872"]
    How would I ensure that the string (which comes from an XML file) is plain text?

    I don't know about any particular library that can unsescape XML/HTML strings. If your input is good enough you can manually remove unwanted tags, but this is not very elegant....

  • Sounds like a manual wrap is not intended for plain text.

    I have to say, QTextOption::ManualWrap raised some expectations, although the documentation does say "Same as QTextOption::NoWrap"

    I don't really get it, I have to admit. Why make a separate enum value that, from it's name, suggests a different behaviour, then say "it's the same as a different enum value".

Log in to reply