]*>\"));You need to strip off the HTML tags from the string. See this post for insta" />
开发者

Get plain text from QString with HTML tags

开发者 https://www.devze.com 2023-03-06 08:05 出处:网络
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 insta

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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消