generate html code from QTableWidget with row and column span support

#include <QApplication>
#include <zftpclient.h>
#include <zpdfviewer.h>
#include <QTableWidget>
#include <QHash>
#include <QFile>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTableWidget tTable;
    tTable.setColumnCount(10);
    tTable.setRowCount(20);
    for(qint32 i=0;i<tTable.rowCount();i++)
    {
        for(qint32 j=0;j<tTable.columnCount();j++)
        {
            QTableWidgetItem *tItem=new QTableWidgetItem;
            tTable.setItem(i,j,tItem);
        }
    }
    tTable.setSpan(0,0,1,3);
    tTable.item(0,0)->setText("(Left,Top)");
    tTable.setSpan(2,2,3,2);
    tTable.item(2,2)->setText("This is beijing hour,open the door to the world.Join us ,couldn't you?");
    tTable.setSpan(13,5,3,6);
    tTable.item(13,5)->setText("hello,this is a html test page ,used to test subtitle parse server.is it work correct?");
    tTable.show();

#if 0
    for(qint32 i=0;i<tTable.rowCount();i++)
    {
        for(qint32 j=0;j<tTable.columnCount();j++)
        {
            qDebug()<<"("<<i<<","<<j<<"):"<<tTable.rowSpan(i,j)<<tTable.columnSpan(i,j);
        }
    }
    qDebug()<<"ends...................";
#endif

    //find out the invalid cells.
    QList<QString> tInvalidCellsList;
    for(qint32 i=0;i<tTable.rowCount();i++)
    {
        for(qint32 j=0;j<tTable.columnCount();j++)
        {
            QString tKey=QString("%1,%2").arg(i).arg(j);
            if(tInvalidCellsList.contains(tKey))
            {
                continue;
            }
            qint32 tRowSpan=tTable.rowSpan(i,j);
            qint32 tColSpan=tTable.columnSpan(i,j);
            if(tRowSpan>1 || tColSpan>1)
            {
                for(qint32 m=i;m<i+tRowSpan;m++)
                {
                    for(qint32 n=j;n<j+tColSpan;n++)
                    {
                        if(m==i && n==j)
                        {
                            //bypass myself.
                            continue;
                        }
                        QString tKey=QString("%1,%2").arg(m).arg(n);
                        tInvalidCellsList.append(tKey);
                    }
                }
            }
        }
    }
    //generate html.
    qreal tColWidth=100.0/tTable.columnCount();
    QString tHtml;
    tHtml.append("<html>");
    tHtml.append(QString("<head><style type=\"text/css\">body{font-size:12px;}table td{width:%1%;height:50px}table tr td{word-break:break-all;}</style></head>").arg(tColWidth));
    tHtml.append("<body>");
    tHtml.append("<table width=\"100%\" height=\"100%\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\">");
    tHtml.append("\n");
    for(qint32 i=0;i<tTable.rowCount();i++)
    {
        tHtml.append(QString("<tr>"));
        tHtml.append("\n");
        for(qint32 j=0;j<tTable.columnCount();j++)
        {
            QString tKey=QString("%1,%2").arg(i).arg(j);
            if(tInvalidCellsList.contains(tKey))
            {
                //bypass invalid cells.
                continue;
            }
            qint32 tRowSpan=tTable.rowSpan(i,j);
            qint32 tColSpan=tTable.columnSpan(i,j);
            tHtml.append(QString("<td rowspan=\"%1\" colspan=\"%2\" align=\"left\">").arg(tRowSpan).arg(tColSpan));
            tHtml.append("<input type=\"checkbox\" checked=\"checked\">");
            tHtml.append(tTable.item(i,j)->text());
            tHtml.append("</td>");
            tHtml.append("\n");
        }
        tHtml.append(QString("</tr>"));
        tHtml.append("\n");
    }
    tHtml.append("</table>");
    tHtml.append("\n");
    tHtml.append("</body>");
    tHtml.append("</html>");
    QFile tFile("/home/shell.albert/subtitle.html");
    if(tFile.open(QIODevice::WriteOnly))
    {
        tFile.write(tHtml.toUtf8());
        tFile.flush();
        tFile.close();
    }
    return 0;
    return a.exec();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值