Nominate our 2022 Qt Champions!

How to detect escape sequence in a user-typed string [solved]

  • I'm just curious if there is a trick for interpreting escape sequences in a TextField.

    In Qt's IDE, I can do something like this:

    @QString x = "hello "bob"";@

    The compile will preprocess the escape sequences so the output is:

    @hello "bob"@

    Which is great. But if I have a situation where the user types a string into a Qt textfield is there a simple solution for converting the string's escape sequences. So if I wanted to type this into the field:

    @line 1\nline 2\nline 3@

    Is there a easy way for QString to detect the escape sequence? Or do I have to parse the string myself in code and reconstruct it with the escape sequences?

  • Moderators


    If you use QTextEdit then "toPlainText()": gives the text as it is.

  • You could use QString.replace() and go through a list of common escape sequences. You need to make sure your search string isn't an escape sequence itself.


    line 1\nline 2\nline 3


  • Rondog, thats the answer I was looking for.

    I had done this exact thing by splitting the string up and rebuiding the string. Your solution is much better!

    I am learning that QString has all kinds of convenient ways of manipulating strings. You just brought a new one to my attention. Thanks!

  • Lifetime Qt Champion


    A regular expression might help you write less code for the replacements

Log in to reply