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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.