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. Replace Parts of Strings with HTML Tags
Forum Update on Monday, May 27th 2025

Replace Parts of Strings with HTML Tags

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.2k 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.
  • E Offline
    E Offline
    enforcer
    wrote on 30 Aug 2013, 08:01 last edited by
    #1

    For my project I want to insert plain text from a QTextEdit into a PDF. I once thought of "it would be nice to make parts of the text bold, italic or as list.
    The plain text is saved as QString within application and in an XML-File. First I thought of writing the plain text with HTML Tags, but the HTML tags would destroy my XML file.

    Finally I had the idea to use the same formating syntax as this forum's text editor has, with asterisks for bold text and lists and underline for italic text. I want to replace this characters for HTML tags so that i can use QTextCursor::insertHtml(QString&) and in PDF I have my bold, italic and list structures.

    The only problem is how to replace this characters with tags. The best way to find text between asterisks etc. is to use regular expression. I need your opinion for my thoughts:

    replace characters like "<", ">" and "&" with HTML writing "&lt;", "&gt;" and "&amp;"

    find "newline, *, space, some text and double newline" and replace them with "<ul>some text</ul>"

    find "newline, *, space, some text and single newline" and replace them with "<li>some text</li>"

    replace "* + some text + *" with "<b>some text</b>"

    replace "_ + some text + _" with "<i>some text</i>"

    What do you think about this?

    lg Enforcer

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 30 Aug 2013, 08:28 last edited by
      #2

      [quote author="enforcer" date="1377849672"]First I thought of writing the plain text with HTML Tags, but the HTML tags would destroy my XML file.[/quote]
      You can use "CDATA":http://www.w3schools.com/xml/xml_cdata.asp in that case to keep the XML valid if thats an option for you.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • E Offline
        E Offline
        enforcer
        wrote on 30 Aug 2013, 09:12 last edited by
        #3

        I'll use that as emergency solution, but I want to make a simple solution for users not familiar with HTML. This forum's text editor uses a pretty simple syntax. I'd like to try this one first.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          enforcer
          wrote on 3 Sept 2013, 13:28 last edited by
          #4

          This forum's text editor uses several regular expressions for replacements. The code can be found when you use your browser (Google Chrome with F12) and check the network's GETs and POSTs. Within the GETs there is JavaScript file where the regex is defined.

          The problem is. it is a ciphered string and only the above designed algorithm can decipher it. Might take some time to get the right regexp.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DerManu
            wrote on 3 Sept 2013, 20:29 last edited by
            #5

            What happens when you enter
            @* bla _ bla * bla _@

            As you see, regular expressions are not sufficient to parse this. You'll also need a method to validate the markup. Just write a small parser and everything will be good.

            1 Reply Last reply
            0

            1/5

            30 Aug 2013, 08:01

            • Login

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