PDA

View Full Version : QString text width without <qt><b> and other special params



Alundra
27th July 2016, 15:02
Hi,
Is it possible to have the text width without the special params as <qt>, <b> and others ?
I wrote an Elided QLabel custom class but then the width is not correct, and also I have this case in other places.
For the simple way I used the remove function of QString to remove <qt>, </qt>, <b> and </b> but there is a way to remove all ? I surely don't know all so I can miss one too.
BUT also italic and bold change the width of the text, so it has to be parsed, but maybe one function already do that in Qt ?
Thanks for the help

anda_skoa
27th July 2016, 16:51
If you want to remove all rich text markup you could try this



QTextDocument doc;
doc.setHtml(yourString);
QString stringWithoutMarkup = doc.toPlainText();


Cheers,
_

d_stranz
27th July 2016, 17:13
Does QFontMetrics handle rich text strings? It doesn't really say so in the docs, but it might be worth checking out.

Alundra
27th July 2016, 18:35
Thanks for the QTextDocument trick but if I set my text "<something>" it's removed, I would only remove special character, is it not possible ?
The goal is for that :

const QString elidedText = fontMetrics().elidedText(textWithoutSpecialParams, Qt::ElideMiddle, width, Qt::TextShowMnemonic);
What I know is <qt>, <b>, <i>, <u>, <font color="">
Maybe the only solution is to do a while loop and remove each part of the string manually

anda_skoa
27th July 2016, 18:57
I thought you wanted the RichText markup removed.

If you only want to remove certain tags then that will be up to you.

Cheers,
_

Alundra
27th July 2016, 23:38
It's exactly that but not "<...>" which is not a known markup like "<not loaded>"

anda_skoa
28th July 2016, 10:56
Hmm, <not loaded> isn't a valid tag, that would have had to be escaped before anyway, no?

I.e. the actual input string would be something like &lt;not loaded&gt;, right?

Does the setHtml/toPlainText() also remove these?

Cheers,
_

Alundra
28th July 2016, 19:53
Using toPlainText(), "<not loaded>" is removed but "&lt;not loaded&gt;" is correcty shown.
Ah yea but then the bold is not shown in the elided text, the elided QLabel doesn't look to be an easy thing to do because it has to not elide params.