insert text with css style into QTextEdit



  • I want to insert a table with a visible grid (aka with border) but I can't seem to find a way to create a table AND style it. I am successful in inserting the <table> <tr> <th> html tags into my QTextEdit using textEdit->insertHtml("<table>.....</table>"); but I cannot style it with textEdit->insertHtml("<table style=\"border: 1px solid black;\">);, the styling just disappear, both in the sense that I cannot see the styling in the QTextEdit AND it disappears in the code that is written to the QTextEdit.

    tl;dl how do you insert a table in QTextEdit AND style it so that it has a border?



  • I've also tried to insert a style tag at the beginning of my document by doing textEdit->setHtml("<style>table, tr, th {border: 1px solid black;}</style>" + defaultPage.readAll()); but the <style> </style> and everything in between gets removed for some reason! I can't see the style tags anywhere inside the code when I open the saved html document.

    I've also read up on how to set stylesheet (Apply stylesheet to HTML content in QTextEdit) and from my understanding, and experience, setting the stylesheet of the textEdit does not work either.

    tl;rd what I've tried:
    (1) inserting a table that is pre-styled with the style="" attribute
    (2) inserting style tags at the beginning of the html with textEdit->setHtml("<style>....</style>" + restOfDocument.readAll());
    (3) changing the stylesheet of the textEdit itself.
    (4) attempting to access something called "defaultStyleSheet" but there is no such function that exists in QTextEdit in my version for some reason.

    None of the above works.

    I am using Qt Creator 4.9.2. Based on Qt 5.13.0 (GCC 9.1.0, 64 bit). I am sure that the version of Qt of the project is 5.13.0 and I am compiling using:
    GCC 9.1.0 on a 64 bit Linux computer
    Qbs version 1.13.1
    QMake version 3.1



  • Update
    I've managed to insert a table using QTextTable but I also want to be able to insert p tags, h tags, etc. with style applied.



  • @adamsmith : I'm trying to get this to work, too, without success so far. QTextEdit just ignores the border, unlike QTableView.

    There is a list of supported CSS styles somewhere in the Qt docs, but I couldn't find any reference to the border style. It is strange if it isn't supported because border was already in CSS level 1, and it is such a basic property.

    This probably has no bearing on the problem, but the CSS reference says that if border-style is not specified, then the border will be invisible. But something like your example (border: 1px solid black;) should work with no problem.



  • I did find a reference to border-color and border-style, but setting these still has no effect in a QTextEdit (AFAICT).



  • SOLVED (???): QTextEdit will recognize the old-style HTML table border attribute, i.e.:

    <table border="1">
    

    However, it doesn't seem to honor CSS properties border-style or border-collapse.

    I suppose we'll just have to live with this, unless someone else can suggest a better way?.



  • @robert-hairgrove
    https://doc.qt.io/qt-5/richtext-html-subset.html# seems to cover what you have mentioned it does and does not support?



  • @jonb Yes, that is the document to which I was referring. Thank you for posting the link!