PDA

View Full Version : Put selected in QTextEdit text into tags



cszawisza
23rd October 2014, 13:39
Hi!

I have a QTextEdit in which I want to implement <s>...</s> html tag (after pushing a button, selected text should be striked) how can I do this properly?

Thanks for answers!

ChrisW67
23rd October 2014, 22:14
It is a shame that the supported HTML subset offers none of the strike, s, or del HTML markup options. The QTextEdit should support using a span with a style using "text-decoration: line-through" So you might be able to get away just doing a search and replace before calling setHtml() (or after toHtml()). Can that achieve what you want?

cszawisza
24th October 2014, 15:56
Not exactly.
What I want to do is add a "strike" option to text editor, so text "this is a very short text" (imagine that bolded text is selected in text editor) after clicking "strike" button editor should change selected text to striked. All I need to do is cut "very short" put it into tags like "<s>very short</s>" and paste it the same point as HTML. But I don't know how to do this, and I don't want to use cut and paste methods, because, when I do so there will be 2 steps to Undo (first "cut" operation and "paste" operation) or that is what I think will happen...

ChrisW67
24th October 2014, 21:31
You probably need to do this by operating directly on the QTextDocument of the editor. When the strikeout button is clicked you merge a QTextCharFormat into the selected text. The text editor example shows how to do this:
http://qt-project.org/doc/qt-5/qtwidgets-richtext-textedit-example.html
Look at the textBold() and mergeFormatOnWordOrSelection() iFunctions n textedit.cpp and use QTextCharFormat::setStrikeout() instead.