Description
I created a TextArea
component in QML, and similar to this example, I created a DocumentHandler class based on a pointer to a QQuickTextDocument
, which is taken through the textDocument property. I need this in order to be able to format the text, that is, make it bold, underlined, italic, strikeOut etc.
What I need
I need to get a text where the formatted parts will be presented as HTML tags.
e.g. Bold text ultimately I would like to get in the form <b>Bold text</b>
. Or for example Bold and italic text I would like to get in the form <b><i>Bold and italic text</i></b>
(the order in which the tags are placed does not matter).
What I tried
I tried to use the toHtml() function, but this function does not suit me because:
- It generates a lot of unnecessary information that I don't need. For example for Bold text it returned the following result:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'Roboto'; font-size:14px; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600;">Bold text</span></p></body></html>
- The usual tags that I need to represent the text (
<b>
,<i>
etc.), this function is formed in the form of thestyle
attribute of the<span>
tag. So it changes the bold with this line:<span style=" font-weight:600;">
.
Description
If I understood correctly, at the moment there is no way to get formatted text with HTML tags without meta information that is generated by the
QTextDocument
using thetoHtml()
function. Therefore, I decided to manually do this work using theQTextCursor
class.Code
I have a structure that provides information about tag:
And I have a
std::vector
of such structures which I initialize as follows:And the most important thing is the
getFormattedText()
function that uses this vector ofTag
objects to return the formatted text. The main idea is to manually place tags in plain text, that is, the opening tag is placed where the formatting begins, and the closing tag is where it ends. Information about where in the text what formatting is used can be taken from theQTextCursor
class, which object we can create based on theQTextDocument
class. As a result, we have the following function:The logic associated with the
currentTextFormat
andlocalTextFormat
variables is necessary in order to "timely" close one combination of formats and open a new one. This combination of formats was named as:Where
FontFormat
is:Functions for getting a
TextFormat
:Functions for getting text interpretation of
TextFormat
:For example, there is such text: a
bc. Each of the letters of this text has a different combination of formats, and when iterating from one of the letters to another, it is necessary to take this into account, closing the old combination and opening a new one.Thus a
bcwill be converted as:<b>a</b><b><s>b</b></s><s>c</s>
.