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 can I get QTextEdit to not alter my HTML?
QtWS25 Last Chance

How can I get QTextEdit to not alter my HTML?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.6k 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.
  • H Offline
    H Offline
    HighMemoryDaemon
    wrote on last edited by HighMemoryDaemon
    #1

    Solution

    I created a library called QBasicHtmlExporter to make this possible.


    Using QTextEdit::setHtml, I set the HTML to the following:

    <h1>1</h1>
    <h2>2</h2>
    <h3>3</h3>
    <p>p</p>
    

    When I call QTextEdit::toHtml later on, all of my heading tags are turned into p tags with inline styling. :(

        <p style=\ " margin-top:18px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\ " font-size:xx-large; font-weight:600;\">1</span></p>\n
        <p style=\ " margin-top:16px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\ " font-size:x-large; font-weight:600;\">2</span></p>\n
        <p style=\ " margin-top:14px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\ " font-size:large; font-weight:600;\">3</span></p>\n
        <p style=\ " margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">p</p>
    

    Any way to get around this?

    My full code: (Ignore the fact that there is no window. This is a tiny piece of code for playing around)

    #include <QApplication>
    #include <QDebug>
    #include <QTextEdit>
    
    int main(int argc, char **argv)
    {
        QApplication a(argc, argv);
    
        QTextEdit *t = new QTextEdit();
    
        t->setHtml("<h1>1</h1><h2>2</h2><h3>3</h3><p>p</p>");
        qDebug() << t->toHtml();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      What version of Qt are you using ?
      On what platform ?

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

      H 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        What version of Qt are you using ?
        On what platform ?

        H Offline
        H Offline
        HighMemoryDaemon
        wrote on last edited by
        #3

        @SGaist Hi! I'm on 5.11.2 and I am on Elementary OS Juno. (Essentially the same thing as Ubuntu 18.04)

        JonBJ 1 Reply Last reply
        0
        • H HighMemoryDaemon

          @SGaist Hi! I'm on 5.11.2 and I am on Elementary OS Juno. (Essentially the same thing as Ubuntu 18.04)

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @HighMemoryDaemon
          http://doc.qt.io/qt-5/qtextedit.html

          If you call setHtml() with legacy HTML, and then call toHtml(), the text that is returned may have different markup, but will render the same.

          The point being, QTextEdit parses your HTML but does not guarantee to retain it, I'm thinking?

          H 1 Reply Last reply
          0
          • JonBJ JonB

            @HighMemoryDaemon
            http://doc.qt.io/qt-5/qtextedit.html

            If you call setHtml() with legacy HTML, and then call toHtml(), the text that is returned may have different markup, but will render the same.

            The point being, QTextEdit parses your HTML but does not guarantee to retain it, I'm thinking?

            H Offline
            H Offline
            HighMemoryDaemon
            wrote on last edited by
            #5

            @JonB Good find. I'm guessing that all text that isn't a list, table or image is converted to <p> and <span> tags.

            Sounds like I might end up having to write a parser of sorts which is unfortunate because it will expand my project's scope.

            I'm thinking the best options I have are:

            • Find a way to loop through the QTextDocument structure and generate proper HTML
            • Some sort of regex replace magic. Might work out.
            • Use an HTML parser library or, I think, QDomDocument, loop through the HTML and make fixes.
            JonBJ 1 Reply Last reply
            0
            • H HighMemoryDaemon

              @JonB Good find. I'm guessing that all text that isn't a list, table or image is converted to <p> and <span> tags.

              Sounds like I might end up having to write a parser of sorts which is unfortunate because it will expand my project's scope.

              I'm thinking the best options I have are:

              • Find a way to loop through the QTextDocument structure and generate proper HTML
              • Some sort of regex replace magic. Might work out.
              • Use an HTML parser library or, I think, QDomDocument, loop through the HTML and make fixes.
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @HighMemoryDaemon
              You are using a QTextEdit to put HTML into and get the same HTML back out of it, to what end? Why the choice of QTextEdit?

              1 Reply Last reply
              0
              • H Offline
                H Offline
                HighMemoryDaemon
                wrote on last edited by
                #7

                I am working on a bi-directional markdown/HTML editor. https://gitlab.com/Open-App-Library/escriba

                When you switch from the rich-text (HTML) mode to the markdown (plain-text) mode, the app must grab the HTML from the QTextEdit, feed it into my HTML-to-Markdown parser, and output the markdown to the markdown editor (Which is a QPlainTextEdit).

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  HighMemoryDaemon
                  wrote on last edited by HighMemoryDaemon
                  #8

                  Hi, all.
                  Just wanted to provide an update that I am working on a solution to this: https://gitlab.com/Open-App-Library/QBasicHtmlExporter

                  It has been a lot easier than expected and I am essentially recreating the QTextHtmlExporter class, what toHtml uses to convert HTML.

                  Currently, you'll notice that it depends on private GUI classes in Qt. It most likely won't by the time this little project is complete. There's one snippet of code that uses private/qtextdocumentfragment_p.h but may be able to be replaced.

                  Willl update this thread again when complete.

                  JonBJ 1 Reply Last reply
                  0
                  • H HighMemoryDaemon

                    Hi, all.
                    Just wanted to provide an update that I am working on a solution to this: https://gitlab.com/Open-App-Library/QBasicHtmlExporter

                    It has been a lot easier than expected and I am essentially recreating the QTextHtmlExporter class, what toHtml uses to convert HTML.

                    Currently, you'll notice that it depends on private GUI classes in Qt. It most likely won't by the time this little project is complete. There's one snippet of code that uses private/qtextdocumentfragment_p.h but may be able to be replaced.

                    Willl update this thread again when complete.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @HighMemoryDaemon
                    I'll just say: to allow users to WYSIWYG edit HTML I use QtWebEnginePage stuff and tell it to make the page editable (i.e. user types in on-page). I don't get involved in any HTML parsing anywhere, nor do I have to write much code. I also add a separate tab where the raw HTML is editable. Is that what you're wanting to provide? It's a rather different approach from yours, and it doesn't use QTextEdit, for right or for wrong.

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      HighMemoryDaemon
                      wrote on last edited by
                      #10

                      Finished! ...As far as I could see. I still have to write some unit tests to be sure there are no major problems. Also added documentation to the readme.

                      @JonB If I knew ahead of time I was going to have to write an html->markdown parser (MarkdownPanda) as well as a special Qt HTML exporter I probably would've went the way of QtWebEngine at least for the minimal viable product version of my app. Too late now, but I am happy with the current setup.

                      I am building a note-taking application called Vibrato Notes and the HTML will not be editable in any way by the user - markdown and rich text will be the two formats of choice. The Escriba editor I mentioned earlier is a fork of MRichTextEditor which paved away a lot of work for me.

                      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