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. CSS styling richtext in QTextDocument
Forum Updated to NodeBB v4.3 + New Features

CSS styling richtext in QTextDocument

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 4.0k Views 1 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.
  • H Offline
    H Offline
    hullatom
    wrote on last edited by hullatom
    #1

    Hello,

    SHORT: Some styles from the supported subset are not working.

    I work in a reporting project. My report contains images tables and graphs. I can successfully add all items. Then I safe it as pdf using Qt, works very nice.

    The main problem is with table styling. According to https://doc.qt.io/qt-5/richtext-html-subset.html the css styles I need should be supported.

    For case of this topic I created minimal demo app using Qt Widgets. On test button it loads css file and template html file. It sets it to the QTextEdit document a and displays.

    What does not work is the width style (nor as attribute) and also separate borders not work, like border-bottom-width: 2px;

    At the end I attach print screens from the app and reference screen from chromium.

    Demo project in a zip file (6kB): https://drive.google.com/file/d/17hRBrIp4NvXo6yIMHmRrdedGQELdwE_8

    Thanks for any hints.

    Regards, Thomas

    Example code:

    mainwindow.cpp :

    void MainWindow::on_pushButton_clicked()
    {
      //https://doc.qt.io/qt-5/richtext-html-subset.html
    
      //open HTML and CSS files
      QFile tstFile("../test.html");
      QFile cssFile("../styles.css");
      tstFile.open(QFile::ReadOnly);
      cssFile.open(QFile::ReadOnly);
    
      //get QTextDocument reference
      QTextDocument *doc = ui->textEdit->document();
    
      //Set CSS and HTML
      doc->setDefaultStyleSheet(cssFile.readAll());
      doc->setHtml(tstFile.readAll());
    
      //Display
      ui->textEdit->setDocument(doc);
    
      //Show Qt version
      ui->label_2->setText(qVersion());
    
      //close files
      tstFile.close();
      cssFile.close();
    
    }
    

    test.html :

    <head>
    	<!-- for use in a classic browser: only for demo, not used in real app -->
    	<link rel="stylesheet" href="./styles.css">
    </head>
    
    <body>
    	
    	<table class="superTable">
    		<tr><td>
    		
    			<table class="tableA"><tr><td>long text, longer than others, right at the top</td></tr></table>
    			
    			<table class="tableB">
    				<tr>
    					<td class="cellA">cell A</td>
    					<td class="cellB">cell B</td>
    				</tr>
    			</table>
    		
    		
    		</td></tr>
    	</table>
    
    </body>
    

    styles.css :

    
    body {
    	color: black;
    	background: yellow;
    
    }
    
    table {
    	border-style: solid;
    	border-width: 1px;
    	border-collapse: collapse;
    }
    
    .superTable {
    	
    	border-top-style: dotted; /* not working */
    	border-bottom-style: dotted; /* not working */
    	
    	border-bottom-width: 2px; /* not working */
    	border-right-width: 2px; /* not working */
    	
    	width: 500px; /* not working */
    	min-width: 500px; /* not working */
    }
    
    .tableA {
    	border: 2px dotted red; /* combined styles not working */
    }
    
    .tableB {
    	width: 100%; /* cannot test as width is not working */
    	
    	align: center; /* not working */
    	text-align: center; /*standart css for browsers*/
    }
    
    .tableB .cellA{
    	width: 30%; /* not working */
    	
    	border-style: solid;
    	border-width: 1px;
    	border-collapse: collapse;
    }
    
    .tableB .cellB {
    	width: 70%; /* not working */
    	
    	border-style: solid;
    	border-width: 1px;
    	border-collapse: collapse;
    }
    
    

    Result :
    testApp.png

    Browser reference:
    browserTest.png

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      Qt does not support all HTML and CSS attributes - see https://doc.qt.io/qt-5/richtext-html-subset.html

      /edit: border-top-style: dotted; should work though... will take a look.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      H 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Qt does not support all HTML and CSS attributes - see https://doc.qt.io/qt-5/richtext-html-subset.html

        /edit: border-top-style: dotted; should work though... will take a look.

        H Offline
        H Offline
        hullatom
        wrote on last edited by
        #3

        @Christian-Ehrlicher Hello, thank you. I know, it is what I say in the question. I use only those mentioned in supported subset as given in the link. Like width. But they do not work for some reason.

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          At least I found the reason why border-style does no longer work - it was due to https://bugreports.qt.io/browse/QTBUG-36152 which added some new capabilities (border-collapse)

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          H 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            At least I found the reason why border-style does no longer work - it was due to https://bugreports.qt.io/browse/QTBUG-36152 which added some new capabilities (border-collapse)

            H Offline
            H Offline
            hullatom
            wrote on last edited by
            #5

            @Christian-Ehrlicher I read it was fixed as of Qt 5.14. But I think I can live with that.

            I found that the width works as attribute. At least the relative % variant.

            Christian EhrlicherC 1 Reply Last reply
            0
            • fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #6

              I struggled with styling the html in QTextDocument. In my code I created an XML representation and then used XSLT stylesheet to convert to HTML. I am not completely satisfed with the output as I had issues making it look the way I wanted. I think I will eventually convert the XML to QML code on the fly. There were so many limitations with the HTML/CSS in QTextDocument it was very frustrating.

              C++ is a perfectly valid school of magic.

              H 1 Reply Last reply
              0
              • fcarneyF fcarney

                I struggled with styling the html in QTextDocument. In my code I created an XML representation and then used XSLT stylesheet to convert to HTML. I am not completely satisfed with the output as I had issues making it look the way I wanted. I think I will eventually convert the XML to QML code on the fly. There were so many limitations with the HTML/CSS in QTextDocument it was very frustrating.

                H Offline
                H Offline
                hullatom
                wrote on last edited by hullatom
                #7

                @fcarney Hello, thanks. Yes, it so limited that the use is only very basic. I am thinking of QML as well. But I never got in touch with that, and it is as learning whole new language. I do not know which way I would have to go. Well new adventure in front of me.

                I am also looking at this report designer: https://sourceforge.net/projects/qtrpt/

                What I like about HTML/CSS, is that everybody knows it, so it can be edited by any body who knows IT basics. I miss just few more styles and I would be happy.

                1 Reply Last reply
                0
                • H hullatom

                  @Christian-Ehrlicher I read it was fixed as of Qt 5.14. But I think I can live with that.

                  I found that the width works as attribute. At least the relative % variant.

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @hullatom said in CSS styling richtext in QTextDocument:

                  I read it was fixed as of Qt 5.14.

                  You read it wrong - I said that this fix introduced the regression about not honoring the border-style when using border-collapse.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  H 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @hullatom said in CSS styling richtext in QTextDocument:

                    I read it was fixed as of Qt 5.14.

                    You read it wrong - I said that this fix introduced the regression about not honoring the border-style when using border-collapse.

                    H Offline
                    H Offline
                    hullatom
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher Maybe, it is somewhere in the text hidden? The summary on top of the page is saying:

                    https://bugreports.qt.io/browse/QTBUG-36152

                    Details
                    Type: Suggestion
                    Status:CLOSED
                    Priority: P4: Low
                    Resolution:Done
                    Affects Version/s:
                    5.2.0
                    Fix Version/s:
                    5.14.0 Alpha

                    Created: 13 Jan '14 22:56
                    Updated: 20 Aug '19 11:07
                    Resolved: 08 Aug '19 17:59

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • H hullatom

                      @Christian-Ehrlicher Maybe, it is somewhere in the text hidden? The summary on top of the page is saying:

                      https://bugreports.qt.io/browse/QTBUG-36152

                      Details
                      Type: Suggestion
                      Status:CLOSED
                      Priority: P4: Low
                      Resolution:Done
                      Affects Version/s:
                      5.2.0
                      Fix Version/s:
                      5.14.0 Alpha

                      Created: 13 Jan '14 22:56
                      Updated: 20 Aug '19 11:07
                      Resolved: 08 Aug '19 17:59

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @hullatom Read:

                      At least I found the reason why border-style does no longer work - it was due to https://bugreports.qt.io/browse/QTBUG-36152 which added some new capabilities (border-collapse)

                      border-style does not work together with border-collapse.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        See https://codereview.qt-project.org/c/qt/qtbase/+/322891

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          hullatom
                          wrote on last edited by
                          #12

                          Thank you all. In the end I managed to use the QtWebEngineView to render full html and css with no limitations.

                          1 Reply Last reply
                          1
                          • R Offline
                            R Offline
                            Rudresh
                            wrote on last edited by
                            #13

                            @hullatom said in CSS styling richtext in QTextDocument:

                            render

                            Hi ,
                            I read something about xml to html conversion but am not too sure
                            and also thread is quite old, but if anyone knows how to convert xml to html
                            ( qt 6.5 ) applying css -xslt , please let me know.

                            thanks & warm regards.
                            ~ Rudresh

                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • R Rudresh

                              @hullatom said in CSS styling richtext in QTextDocument:

                              render

                              Hi ,
                              I read something about xml to html conversion but am not too sure
                              and also thread is quite old, but if anyone knows how to convert xml to html
                              ( qt 6.5 ) applying css -xslt , please let me know.

                              thanks & warm regards.
                              ~ Rudresh

                              Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Rudresh said in CSS styling richtext in QTextDocument:

                              ( qt 6.5 ) applying css -xslt , please let me know.

                              You have to use an external library for this. Qt does not provide this functionality in Qt6.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              R 1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher

                                @Rudresh said in CSS styling richtext in QTextDocument:

                                ( qt 6.5 ) applying css -xslt , please let me know.

                                You have to use an external library for this. Qt does not provide this functionality in Qt6.

                                R Offline
                                R Offline
                                Rudresh
                                wrote on last edited by
                                #15

                                @Christian-Ehrlicher
                                Thanks a lot Christian.

                                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