Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I get QTextEdit to not alter my HTML?



  • 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();
    }
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



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



  • @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?



  • @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.


  • @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?



  • 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).



  • 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.



  • @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.



  • 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.


Log in to reply