Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to detect escape sequence in a user-typed string [solved]

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kloveridge
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      If you use QTextEdit then "toPlainText()":http://doc.qt.io/qt-5/qtextedit.html#plainText-prop gives the text as it is.

      157

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rondog
        wrote on last edited by
        #3

        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.

        Example:

        @
        line 1\nline 2\nline 3

        line_1.replace("\n","\n");
        line_1.replace("\t","\t");
        etc.
        @

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kloveridge
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved