[Qt-interest] auto closing xml tags
Mark Summerfield
list at qtrac.plus.com
Mon Aug 23 09:05:34 CEST 2010
On Sun, 22 Aug 2010 22:39:18 +0200
Nesbitt <none at none.no> wrote:
> Hi
>
> I am using QTextEdit as xml editor. I'd like to implement auto
> closing tags.
>
> For example when user types <tag1 arg1="val1"> QTextEdit should
> insert </tag1> after cursor.
>
> I've read QCompleter docs and as I understood it I cannot use it
> because there is no list of valid words (every string could be a tag
> name).
>
> What is the best way to do this?
One approach you might try is to subclass QTextEdit and reimplement the
keyPress() event handler. In that handler detect the insertion of a '>'
character. Whether you detect it or not, call the base class to handle
whatever key was pressed. And then, if '>' was entered use a QTextCursor
to select, say, to the start of the paragraph & copy it into a string.
Then use a regex to see if you have a match for <(tag)[^>]*> and if you
do extract the tag name and insert the closing tag. (I'd also select the
newly inserted tag so that if the user doesn't want it they can hit
Backspace to delete it.)
BTW I haven't tried this, so no guarantees:-)
PS You might like to look at the xmledit widget in the examples for
Advanced Qt Programming since you might be able to combine ideas in it
with your own.
--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Advanced Qt Programming" - ISBN 0321635906
http://www.qtrac.eu/aqpbook.html
More information about the Qt-interest-old
mailing list