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. toHTML without header?
Forum Update on Monday, May 27th 2025

toHTML without header?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 2.1k 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.
  • A Offline
    A Offline
    angela2016
    wrote on last edited by
    #1

    Hi,

    I'm new to C++ and Qt and I'm writing some code where I create an html document containing text from a couple of different widgets. So I'd like to retrieve the html string from a QTextEdit without the headers and footers. So without the "<!DOCTYPE HTML PUBLIC" etc... (because I'm including those myself) but including all tags pertaining to the string itself (like colors etc...).

    Is this possible without manually replacing everything I don't need?

    Thanks in advance.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      As far as I know, you can only get the full html.

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        I did not test it but you could try iterating the text blocks:

        QTextDocument* doc= textEdit->document();
          for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
                qDebug() << it.text();
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        A 1 Reply Last reply
        1
        • VRoninV VRonin

          I did not test it but you could try iterating the text blocks:

          QTextDocument* doc= textEdit->document();
            for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
                  qDebug() << it.text();
          
          A Offline
          A Offline
          angela2016
          wrote on last edited by
          #4

          @VRonin
          No, that just returns plain text, unfortunately.

          A 1 Reply Last reply
          0
          • A angela2016

            @VRonin
            No, that just returns plain text, unfortunately.

            A Offline
            A Offline
            ambershark
            wrote on last edited by ambershark
            #5

            @angela2016 You're going to have to strip out the html you don't want. Your only 2 options from that control are text() and toHtml(). It should be nice and easy since you know what you added so just use a QString to remove the stuff you don't want.

            I.e.

            auto text = textEdit->toHtml();
            text = text.remove(myHeader);
            text = text.remove(myFooter);
            

            Be careful though as remove will remove all instances so if your header is something simple like "<div>" it will remove all "<div>"'s that it can find in the string.

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            1 Reply Last reply
            2
            • A Offline
              A Offline
              angela2016
              wrote on last edited by
              #6

              Yes, I guess I'll add some function to do that.

              But I don't understand what you mean by "header you wrote". Is there a way to insert your own header into QTextDocument? Because the one I'm talking about is generated automatically.

              A 1 Reply Last reply
              0
              • A angela2016

                Yes, I guess I'll add some function to do that.

                But I don't understand what you mean by "header you wrote". Is there a way to insert your own header into QTextDocument? Because the one I'm talking about is generated automatically.

                A Offline
                A Offline
                ambershark
                wrote on last edited by
                #7

                @angela2016 said in toHTML without header?:

                Yes, I guess I'll add some function to do that.

                But I don't understand what you mean by "header you wrote". Is there a way to insert your own header into QTextDocument? Because the one I'm talking about is generated automatically.

                Oh I thought it was a custom one you were adding. And yes you can change any html in any control in Qt. :) If it's the Qt generated html you could just leave it alone, maybe I don't understand your desire to not have the header returned.

                But you can definitely modify the outputted html to suit your needs if you don't want the full html for some reason. You can use my method, or if you are going to do it a lot just derive your own control and override the toHtml() function to strip the parts you don't want.

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                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