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. QTextLayout can be used with ... rich text. No it can't.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 6 Posters 2.3k Views 3 Watching
  • 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.
  • mcmayerM Offline
    mcmayerM Offline
    mcmayer
    wrote on last edited by
    #1

    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 ;-)

    jsulmJ 1 Reply Last reply
    0
    • F Offline
      F Offline
      Fuel
      wrote on last edited by Fuel
      #2

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

      1 Reply Last reply
      0
      • mcmayerM Offline
        mcmayerM Offline
        mcmayer
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Fuel
          wrote on last edited by
          #4

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

          mcmayerM 1 Reply Last reply
          2
          • mcmayerM mcmayer

            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 ;-)

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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 :-)

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • F Fuel

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

              mcmayerM Offline
              mcmayerM Offline
              mcmayer
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • MrKozmonM Offline
                MrKozmonM Offline
                MrKozmon
                wrote on last edited by MrKozmon
                #7

                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.

                J 1 Reply Last reply
                1
                • MrKozmonM MrKozmon

                  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.

                  J Offline
                  J Offline
                  JoshG
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi and welcome to devnet,

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

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

                    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