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

How to align a table created using HTML to center



  • I am trying to create a table that can be printed in a html format file . and its printing but the only concern is that its printing to the left of file and i want it at the center of file . What code should be added to make it print at the center of the page?
    Below is the code which i have used to print the table

    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=\"50%\"  cellspacing=\"0\" class=\"tbl\">";
             text1 +=("<tr><td>"+qfont+"</td><td>" + name) +"</td>");
             text1 +=("<tr><td>"+qTestedBy+"</td><td>" +TestedBy+"</td>");
             text1 +=("<tr><td>"+qCurrentDate+"</td><td>" + QDate::currentDate().toString() +"</td>");
             text1 +=("<tr><td>"+qCurrentTime+"</td><td>" + QTime::currentTime().toString() +"</td>");
    

    QFile qHtmlFile(qFileName);
    if (qHtmlFile.exists())
    {
    if(qHtmlFile.open(QIODevice::ReadWrite))
    {
    QTextStream qData(&qHtmlFile);
    qRep = qIndex+qAll+css+text
    qTextRep.document()->setHtml(qRep);
    qTextRep.setFont(qFon);
    qData << qTextRep.document()->toHtml();

                  qDebug("Report Created");
            
        }

  • Qt Champions 2019

    @ManiRon said in How to align a table created using HTML to center:

    its printing to the left of file and i want it at the center of file

    I don't understand: do you mean it is left aligned when viewing the HTML file in a web browser?
    And what exactly is left aligned: the content of the cells or the table itself?



  • @jsulm

    the table itself and i want to align it to the center of the page


  • Qt Champions 2019

    @ManiRon So, in the web browser? In this case I don't see how it is related to Qt, you need to find out how to center a table in HTML.
    What you posted doesn't even look like HTML (where is <html></html>?).



  • @jsulm

    I am just using the HTML format to create so i store the things in QString variable and print it


  • Qt Champions 2019

    @ManiRon As I said, your problem is more related to HTML not Qt. I'm not an HTML expert, so don't know how to center a table. Maybe https://www.w3schools.com/tags/att_table_align.asp ?



  • @jsulm I saw that and tried it but its not center align the table



  • @ManiRon
    To align table centrally horizontally, use one of the following:

    • <table style="margin-left: auto; margin-right: auto;">
    • <table style="margin-left: 0; margin-right: auto;">
    • Put table inside <div></div> and align that.

    The above may assume you have something like html, body { width: 100%; } so that it knows what to center within, I'm not sure, you might have to play.


Log in to reply