PDA

View Full Version : How to replace words while retaining their format in QTextEdit



KrishnaPG
13th March 2009, 15:38
Hello,

I need to replace words of the selected text in the QTextEdit control (I have to encode each word independently to some equivalent word). While doing so I should not loose the character format.
How to achieve this effectively?

In other words, I need to be able to iterate over the formatted fragments of the selected text one by one.

Currently I am using textCursor().selection().toHtml() and constructing a QDomDocument and using the elementsByTagName("span").
This is giving me the nodes, each one holding a childnode containing the text (words) that all share one character format.

When I replace those nodeValues and put the DomDocument's html back to selection cursor it works fine.

However, the biggest problem with this approach is 'Table cells'. When only few table cells are selected, the selection.toHtml() creates html with a complete table (instead of only table cells), which when put back to the selection cursor, will insert a *new* table inside the cells, instead of merging with the original parent table.

My question is:
How to identify table cells in the selected text and replace those cells' text words correctly (retaining the table format) ?

Thank you,
P.Gopalakrishna
http://www.CineFxLabs.com/