how to create a sample HTML file using QFIle
-
Hi,
Use of QFile:
- Create QFile variable
- Call
open
- Check that
open
was successful - Call write
- Close file
Done
-
this whole code will be stored in a QString and this i am printing
<article><div align="right"></div><header><h1><center><font color="midnightblue"></font></center></h1><h2><font color="dimgray"></font></h2></header></article><table border="1" style="background-color:ghostwhite;border:1px dotted black;width:80%;border-collapse:collapse;"><tr style="background-color:gainsboro;color:black;"></table><style type="text/css">table.tbl {border-width: 1px;border-style: solid;border-color: black;margin-top: 0px;margin-bottom: 0px;color: black;}table.tbl td {padding: 3px;}table.tbl th {padding: 3px;font-size: 18px;}</style><table width="100%" cellspacing="0" class="tbl" style="border:1px solid black;border-collapse:collapse;" align = center ><tr><th><b><center><span style =font-size:10pt;>Application</span></center></b></th><th><b><center><span style =font-size:10pt;>Check Sum</span></center></b></th><th><b><center><span style =font-size:10pt;>Total</span></center></b></th><th><b><center><span style =font-size:10pt;>No</span></center></b></th><th><b><center><span style =font-size:10pt;>Tested By</span></center></b></th><th><b><center><span style =font-size:10pt;></span></center></b></th><th><b><center><span style =font-size:10pt;></span></center></b></th></tr><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;></span></center></td><td><center><span style =font-size:10pt;></span></center></td><td><center><span style =font-size:10pt;></span></center></td><td><center><span style =font-size:10pt;></span></center></td></tr></table><br><br><br><style type="text/css">table.tbl1 {border-width: 1px;border-style: solid;border-color: black;margin-top: 0px;margin-bottom: 0px;color: black;}table.tbl1 td {padding: 3px;}table.tbl1 th {padding: 3px;font-size: 18px;}</style><table width="100%" cellspacing="0" class="tbl1"><tr><th>Name</th><th>Data</th><th>data</th><th>Test Result</th></tr><tr><td><b><span style =font-size:10pt;>sss</span></b></td><td>Data1</td><td>Data2</td><td><b><span style =font-size:10pt;><font color=Green>PASS</font></span></b></td></tr></table><br><br><br><br><hr><br><br>
-
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(); -
Since you are hard coding stuff anyway, you can use
QXmlStreamWriter
directly on theQFile
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 }
-
Did you try to compare the HTML you generated with QTextBrowser and the one you are generating now ?