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

QTextLayout can be used with ... rich text. No it can't.



  • The documentation of QTextLayout states that it

    can be used with both plain and rich text.

    I'd like to challenge this statement. The setText function does not render rich text, and something like setHtml, which would be the standard way to pass rich text, does not exist. Nor does something like setTextFormat, as is used in QStaticText. In all, I claim that QTextLayout does not render rich text.
    Please prove me wrong ;-)



  • QTextLayout has a Method called setTextOption() with this Option you can set RichText Properties. Look here http://doc.qt.io/qt-5/qtextoption.html



  • Correct me if I'm wrong: QTextOption let's me set a very small number of properties such as line-wrap, tab display, text direction, alignment, etc. This is not rich text, they are just "general rich text properties".
    Maybe my mistake is to equate rich text with the supported HTML subset.



  • Why you want to use it? For your need i can recommend a QTextDocument. It has also a Draw Method


  • Lifetime Qt Champion

    @mcmayer If there is something wrong in Qt or its documentation then you should file a bug here https://bugreports.qt.io/secure/Dashboard.jspa
    And you can even contribute a patch if you like :-)



  • @Fuel I want to find a solution to the problem of paginating a QTextDocument. So far I haven't figured out to do it with the higher level functions, so I'm digging deeper, now.



  • For those who are still looking for a solution to the problem stated above

    As far as I know QTextLayout still doesn't have any setHtml kinda useful function to fill itself with html content. But QTextDocument has one. Indeed if you look at its codes, it uses a utility class called QTextHtmlImporter to import html content (more info). That class parses html content and sets appropriate formats for each block on document's internal QTextLayout. So QTextDocument could be used to obtain QTextLayout for each block that is filled with appropriate html content.

    For example:

    QPainter p(this);
    
    // Set html content on document
    QTextDocument doc;
    doc.setHtml("<p><b>Ready</b> <u><i>for</i></u> Test</p>");
    
    // Obtain layout of the first block
    QTextLayout* layout = doc.firstBlock().layout();
    
    // Paint the first line of the first block
    layout->beginLayout();
    QTextLine line = layout->createLine();
    line.setLineWidth(100);
    line.setPosition({0, 0});
    layout->endLayout();
    layout->draw(&p, {0, 0});
    

    You can also iterate on other blocks and lines for a complete document paint.



  • @MrKozmon said in QTextLayout can be used with ... rich text. No it can't.:

    For those who are still looking for a solution to the problem stated above

    As far as I know QTextLayout still doesn't have any setHtml kinda useful function to fill itself with html content. But QTextDocument has one. Indeed if you look at its codes, it uses a utility class called QTextHtmlImporter to import html content (more info). That class parses html content and sets appropriate formats for each block on document's internal QTextLayout. So QTextDocument could be used to obtain QTextLayout for each block that is filled with appropriate html content.

    I just ran into this as well. The docs say multiple times that QTextLayout supports Rich Text, but there is no setHtml or way to set to rich text. Is there any fix in sight for this other than using QTextDocument (seems like a hack...).


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    That's likely something you should check the bug report system for.


Log in to reply