Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to create a sample HTML file using QFIle
Qt 6.12 LTS is out! See what's new in the release blog

how to create a sample HTML file using QFIle

Scheduled Pinned Locked Moved Solved General and Desktop
28 Posts 5 Posters 12.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.
  • jsulmJ jsulm

    @ManiRon This is not valid HTML.
    And by "code" I mean your C++ code to generate the HTML file...

    ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by ManiRon
    #18

    @jsulm

    QString qHostVer = "<b><center><span style =font-size:10pt;>Application</span></center></b>";

    QString qCheckSum = "<b><center><span style =font-size:10pt;>123</span></center></b>";

    QString qTotal= "<b><center><span style =font-size:10pt;>Total</span></center></b>";

    QString qNo= "<b><center><span style =font-size:10pt;>No</span></center></b>";

    QString qTestedBy = "<b><center><span style =font-size:10pt;>Tested By</span></center></b>";

             css1 = "<style type=\"text/css\">";
             css1 += "table.tbl {border-width: 1px;border-style: solid;border-color: black;margin-top: 0px;margin-bottom: 0px;color: black;}";
             css1 += "table.tbl td {padding: 3px;}";
             css1 += "table.tbl th {padding: 3px;font-size: 18px;}";
             css1+="</style>";
             text1 += "<table width=\"100%\" cellspacing=\"0\" class=\"tbl\" style=\"border:1px solid black;border-collapse:collapse;\" align = center >";
             text1 +=("<tr><th>"+qHostVer+"</th><th>"+qCheckSum+"</th><th>"+qTargetCheckSum+"</th>"\
                      "<th>"+qfont+"</th><th>"+qTestedBy+"</th></tr>");
            QString  text1 +=("<tr><td><center><span style =font-size:10pt;>" + 1.00 +"</span></center></td>"\
                      "<td><center><span style =font-size:10pt;>" + 123 +"</span></center></td>"\
                      "<td><center><span style =font-size:10pt;>" +  +"</span></center></td>"\
                      "<td><center><span style =font-size:10pt;>" + o +"</span></center></td>"\
                      "<td><center><span style =font-size:10pt;>"+ +"</span></center></td>");
    

    qRep = css+text;

    QFile qHtmlFile(file path);
    qHtmlFile.write(qRep);
    qHtmlFile.close();

    jsulmJ 1 Reply Last reply
    0
    • jsulmJ jsulm

      @ManiRon This is not valid HTML.
      And by "code" I mean your C++ code to generate the HTML file...

      ManiRonM Offline
      ManiRonM Offline
      ManiRon
      wrote on last edited by
      #19

      @jsulm this code works fine when i use QTextbrowser, But i dont want to use QTextBrowser as it cannot be used inside a QThread

      1 Reply Last reply
      0
      • ManiRonM ManiRon

        @jsulm

        QString qHostVer = "<b><center><span style =font-size:10pt;>Application</span></center></b>";

        QString qCheckSum = "<b><center><span style =font-size:10pt;>123</span></center></b>";

        QString qTotal= "<b><center><span style =font-size:10pt;>Total</span></center></b>";

        QString qNo= "<b><center><span style =font-size:10pt;>No</span></center></b>";

        QString qTestedBy = "<b><center><span style =font-size:10pt;>Tested By</span></center></b>";

                 css1 = "<style type=\"text/css\">";
                 css1 += "table.tbl {border-width: 1px;border-style: solid;border-color: black;margin-top: 0px;margin-bottom: 0px;color: black;}";
                 css1 += "table.tbl td {padding: 3px;}";
                 css1 += "table.tbl th {padding: 3px;font-size: 18px;}";
                 css1+="</style>";
                 text1 += "<table width=\"100%\" cellspacing=\"0\" class=\"tbl\" style=\"border:1px solid black;border-collapse:collapse;\" align = center >";
                 text1 +=("<tr><th>"+qHostVer+"</th><th>"+qCheckSum+"</th><th>"+qTargetCheckSum+"</th>"\
                          "<th>"+qfont+"</th><th>"+qTestedBy+"</th></tr>");
                QString  text1 +=("<tr><td><center><span style =font-size:10pt;>" + 1.00 +"</span></center></td>"\
                          "<td><center><span style =font-size:10pt;>" + 123 +"</span></center></td>"\
                          "<td><center><span style =font-size:10pt;>" +  +"</span></center></td>"\
                          "<td><center><span style =font-size:10pt;>" + o +"</span></center></td>"\
                          "<td><center><span style =font-size:10pt;>"+ +"</span></center></td>");
        

        qRep = css+text;

        QFile qHtmlFile(file path);
        qHtmlFile.write(qRep);
        qHtmlFile.close();

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #20

        @ManiRon You're not generating valid HTML

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

        ManiRonM 1 Reply Last reply
        0
        • jsulmJ jsulm

          @ManiRon You're not generating valid HTML

          ManiRonM Offline
          ManiRonM Offline
          ManiRon
          wrote on last edited by
          #21

          @jsulm so what change i should make

          jsulmJ 1 Reply Last reply
          0
          • ManiRonM ManiRon

            @jsulm so what change i should make

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #22

            @ManiRon This is how a HTML document should look like:

            <html>
              <head>
               HEADER IF NEEDED 
              </head>
              <body>
                YOUR CONTENT HERE
              </body>
            </html>
            

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

            ManiRonM 1 Reply Last reply
            1
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #23

              Since you are hard coding stuff anyway, you can use QXmlStreamWriter directly on the QFile device to generate html:

              QFile qHtmlFile("myFile.html");
              if(qHtmlFile.open(QIODevice::WriteOnly){
              QTextStream startDocStream(&qHtmlFile);
              startDocStream << QStringLiteral("<!DOCTYPE html>");
              QXmlStreamWriter htmlWriter(&qHtmlFile);
              htmlWriter.writeStartElement(QStringLiteral("html"));
              htmlWriter.writeAttribute(QStringLiteral("xmlns"),QStringLiteral("http://www.w3.org/1999/xhtml"));
              htmlWriter.writeAttribute(QStringLiteral("lang"),QStringLiteral("en"));
              htmlWriter.writeAttribute(QStringLiteral("xml"),QStringLiteral("lang"),QStringLiteral("en"));
              htmlWriter.writeStartElement(QStringLiteral("head"));
              htmlWriter.writeStartElement(QStringLiteral("meta"));
              htmlWriter.writeAttribute(QStringLiteral("http-equiv"),QStringLiteral("Content-Type"));
              htmlWriter.writeAttribute(QStringLiteral("content"),QStringLiteral("text/html; charset=utf-8"));
              htmlWriter.writeEndElement(); //meta
              htmlWriter.writeStartElement(QStringLiteral("title"));
              htmlWriter.writeCharacters(QStringLiteral("Test Page"));
              htmlWriter.writeEndElement(); //title
              htmlWriter.writeStartElement(QStringLiteral("style"));
              htmlWriter.writeCharacters(QStringLiteral("h1, h2, h3, h4 { color: rgb(83,129,53) } h1 { text-align: center; }"));
              htmlWriter.writeEndElement(); //style
              htmlWriter.writeEndElement(); //head
              htmlWriter.writeStartElement(QStringLiteral("body"));
              htmlWriter.writeStartElement(QStringLiteral("h1"));
              htmlWriter.writeCharacters(QStringLiteral("Test Page"));
              htmlWriter.writeEndElement(); //h1
              htmlWriter.writeStartElement(QStringLiteral("h2"));
              htmlWriter.writeCharacters(QStringLiteral("Lorem"));
              htmlWriter.writeEndElement(); //h2
              htmlWriter.writeStartElement(QStringLiteral("p"));
              htmlWriter.writeCharacters(QStringLiteral("Lorem ipsum dolor sit amet, consectetur adipiscing elit."));
              htmlWriter.writeEndElement(); //p
              htmlWriter.writeEndElement(); //body
              htmlWriter.writeEndElement(); //html
              }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              ManiRonM 2 Replies Last reply
              5
              • jsulmJ jsulm

                @ManiRon This is how a HTML document should look like:

                <html>
                  <head>
                   HEADER IF NEEDED 
                  </head>
                  <body>
                    YOUR CONTENT HERE
                  </body>
                </html>
                
                ManiRonM Offline
                ManiRonM Offline
                ManiRon
                wrote on last edited by
                #24

                @jsulm I have not defined the html tag but still it was generating the html file correctly but when i removed the QTextbrowser , still it was generating correctly but the internal borders of the table is only the major missing.

                1 Reply Last reply
                0
                • VRoninV VRonin

                  Since you are hard coding stuff anyway, you can use QXmlStreamWriter directly on the QFile device to generate html:

                  QFile qHtmlFile("myFile.html");
                  if(qHtmlFile.open(QIODevice::WriteOnly){
                  QTextStream startDocStream(&qHtmlFile);
                  startDocStream << QStringLiteral("<!DOCTYPE html>");
                  QXmlStreamWriter htmlWriter(&qHtmlFile);
                  htmlWriter.writeStartElement(QStringLiteral("html"));
                  htmlWriter.writeAttribute(QStringLiteral("xmlns"),QStringLiteral("http://www.w3.org/1999/xhtml"));
                  htmlWriter.writeAttribute(QStringLiteral("lang"),QStringLiteral("en"));
                  htmlWriter.writeAttribute(QStringLiteral("xml"),QStringLiteral("lang"),QStringLiteral("en"));
                  htmlWriter.writeStartElement(QStringLiteral("head"));
                  htmlWriter.writeStartElement(QStringLiteral("meta"));
                  htmlWriter.writeAttribute(QStringLiteral("http-equiv"),QStringLiteral("Content-Type"));
                  htmlWriter.writeAttribute(QStringLiteral("content"),QStringLiteral("text/html; charset=utf-8"));
                  htmlWriter.writeEndElement(); //meta
                  htmlWriter.writeStartElement(QStringLiteral("title"));
                  htmlWriter.writeCharacters(QStringLiteral("Test Page"));
                  htmlWriter.writeEndElement(); //title
                  htmlWriter.writeStartElement(QStringLiteral("style"));
                  htmlWriter.writeCharacters(QStringLiteral("h1, h2, h3, h4 { color: rgb(83,129,53) } h1 { text-align: center; }"));
                  htmlWriter.writeEndElement(); //style
                  htmlWriter.writeEndElement(); //head
                  htmlWriter.writeStartElement(QStringLiteral("body"));
                  htmlWriter.writeStartElement(QStringLiteral("h1"));
                  htmlWriter.writeCharacters(QStringLiteral("Test Page"));
                  htmlWriter.writeEndElement(); //h1
                  htmlWriter.writeStartElement(QStringLiteral("h2"));
                  htmlWriter.writeCharacters(QStringLiteral("Lorem"));
                  htmlWriter.writeEndElement(); //h2
                  htmlWriter.writeStartElement(QStringLiteral("p"));
                  htmlWriter.writeCharacters(QStringLiteral("Lorem ipsum dolor sit amet, consectetur adipiscing elit."));
                  htmlWriter.writeEndElement(); //p
                  htmlWriter.writeEndElement(); //body
                  htmlWriter.writeEndElement(); //html
                  }
                  
                  ManiRonM Offline
                  ManiRonM Offline
                  ManiRon
                  wrote on last edited by
                  #25

                  @VRonin this might make it a little tough i think so sir?

                  1 Reply Last reply
                  0
                  • VRoninV VRonin

                    Since you are hard coding stuff anyway, you can use QXmlStreamWriter directly on the QFile device to generate html:

                    QFile qHtmlFile("myFile.html");
                    if(qHtmlFile.open(QIODevice::WriteOnly){
                    QTextStream startDocStream(&qHtmlFile);
                    startDocStream << QStringLiteral("<!DOCTYPE html>");
                    QXmlStreamWriter htmlWriter(&qHtmlFile);
                    htmlWriter.writeStartElement(QStringLiteral("html"));
                    htmlWriter.writeAttribute(QStringLiteral("xmlns"),QStringLiteral("http://www.w3.org/1999/xhtml"));
                    htmlWriter.writeAttribute(QStringLiteral("lang"),QStringLiteral("en"));
                    htmlWriter.writeAttribute(QStringLiteral("xml"),QStringLiteral("lang"),QStringLiteral("en"));
                    htmlWriter.writeStartElement(QStringLiteral("head"));
                    htmlWriter.writeStartElement(QStringLiteral("meta"));
                    htmlWriter.writeAttribute(QStringLiteral("http-equiv"),QStringLiteral("Content-Type"));
                    htmlWriter.writeAttribute(QStringLiteral("content"),QStringLiteral("text/html; charset=utf-8"));
                    htmlWriter.writeEndElement(); //meta
                    htmlWriter.writeStartElement(QStringLiteral("title"));
                    htmlWriter.writeCharacters(QStringLiteral("Test Page"));
                    htmlWriter.writeEndElement(); //title
                    htmlWriter.writeStartElement(QStringLiteral("style"));
                    htmlWriter.writeCharacters(QStringLiteral("h1, h2, h3, h4 { color: rgb(83,129,53) } h1 { text-align: center; }"));
                    htmlWriter.writeEndElement(); //style
                    htmlWriter.writeEndElement(); //head
                    htmlWriter.writeStartElement(QStringLiteral("body"));
                    htmlWriter.writeStartElement(QStringLiteral("h1"));
                    htmlWriter.writeCharacters(QStringLiteral("Test Page"));
                    htmlWriter.writeEndElement(); //h1
                    htmlWriter.writeStartElement(QStringLiteral("h2"));
                    htmlWriter.writeCharacters(QStringLiteral("Lorem"));
                    htmlWriter.writeEndElement(); //h2
                    htmlWriter.writeStartElement(QStringLiteral("p"));
                    htmlWriter.writeCharacters(QStringLiteral("Lorem ipsum dolor sit amet, consectetur adipiscing elit."));
                    htmlWriter.writeEndElement(); //p
                    htmlWriter.writeEndElement(); //body
                    htmlWriter.writeEndElement(); //html
                    }
                    
                    ManiRonM Offline
                    ManiRonM Offline
                    ManiRon
                    wrote on last edited by ManiRon
                    #26

                    @VRonin @jsulm any other better way if possible with the same way in which i have done

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

                      Did you try to compare the HTML you generated with QTextBrowser and the one you are generating now ?

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

                      ManiRonM 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        Did you try to compare the HTML you generated with QTextBrowser and the one you are generating now ?

                        ManiRonM Offline
                        ManiRonM Offline
                        ManiRon
                        wrote on last edited by
                        #28

                        @SGaist it worked when i added "table.tbl td {border: 2px solid black;}, table.tbl th {border: 2px solid black;}" these two line .

                        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
                        • Unsolved