I have QString wi开发者_开发技巧th html tags. Why can i get plain text from this string?
str.remove(QRegExp("<[^>]*>"));
You need to strip off the HTML tags from the string. See this post for instance.
The question is a bit dusty now, but for anyone checking, it is (has become?) possible to use QTextDocumentFragment
to convert HTML to plaintext in the Qt way.
This approach may offer consistency dependending on your purpose, as well as robustness (of course depending on the Qt folks time :) - but their user base is sizable, and so far it has worked perfectly for me).
I tried something like the below out in Python, I think the mechanics should be about the same in C++ too.
from PySide2.QtGui import QTextDocumentFragment
plain = QTextDocumentFragment.fromHtml(
"<body>someHtml<br/><b>it was so booold!</b></body>"
).toPlainText()
(Additionally, the Qt documentation suggests that you can just pass in the QString
to fromHtml
there.)
精彩评论