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. [solved] Going through the links (<a> tags) in a QTextEdit
Forum Updated to NodeBB v4.3 + New Features

[solved] Going through the links (<a> tags) in a QTextEdit

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.6k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    silviubogan
    wrote on last edited by
    #1

    I have a QTextEdit in which the user can insert text and links (<a>, anchors). I need a way to loop through the links entered by the user and also to know their context (the text before and after every link). I tried going through all the QTextBlocks in the corresponding QTextDocument and use the QTextBlock::text() method but what I see are the lines in the QTextEdit and the links are converted to plain text. What I want to do with the links is to make a function similar with QTextEdit::toPlainText but which prints links in the text in a different way, such as {{http://www.google.com/}} (between two {{ and }} for example).

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      An ugly regexp might do the job:
      @
      auto text = textEdit->toHtml();
      QRegExp exp("<\sa\s+[^>]href\s=\s\"([^\"])\"\s>(.)<\/\sa\s*>");
      text.replace(exp, "{{\1}}");
      textEdit->setHtml(text);
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        silviubogan
        wrote on last edited by
        #3

        Thank you. Your code worked well. I also used this statement
        @exp.setMinimal(true);@

        before this one:
        @text.replace(exp, "{{\1}}");@

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          Thanks for posting your solution here for the benefit of others.

          Please add [solved] to the topic so everyone can see that this problem is solved. You can do that by editing your first post.

          Qt Certified Specialist
          www.edalsolutions.be

          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