QTableView打印 [英] QTableView printing

查看:654
本文介绍了QTableView打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我如何做到这一点?



谢谢很多

解决方案

这是摆脱中间文件的第一个答案的变体。

  QString strStream; 
QTextStream out(& strStream);

const int rowCount = pPublic-> tableView-> model() - > rowCount();
const int columnCount = pPublic-> tableView-> model() - > columnCount();

out<< < html> \ n
< head> \\\

< meta Content = \Text / html; charset = Windows-1251 \> \\\

<< QString(< title>%1< / title> \\\
)。arg(strTitle)
& < / head> \ n
< body bgcolor =#ffffff link =#5000A0> \\\

< table border = 1 cellspacing = 0 cellpadding = 2& \\ n;

// headers
out<< < thead>< tr bgcolor =#f0f0f0>;
for(int column = 0; column< columnCount; column ++)
if(!pPublic-> tableView-> isColumnHidden(column))
out< QString(< th>%1" / th>)arg(pPublic-> tableView-> model() - > headerData(column,Qt:Horizo​​ntal).toString());
out<< < / tr>< / thead> \\\
;

//数据表
for(int row = 0; row< rowCount; row ++){
out< < tr>;
for(int column = 0; column< columnCount; column ++){
if(!pPublic-> tableView-> isColumnHidden(column)){
QString data = pPublic-> ; tableView-> model() - > data(pPublic-> tableView-> model() - > index(row,column))toString()。
out<< QString(< td bkcolor = 0>%1< / td>)arg((!data.isEmpty())?data:QString(& nbsp;));
}
}
out<< < / tr> \\\
;
}
out<< < / table> \\\

< / body> \\\

< / html> \\\


QTextDocument * document = new QTextDocument();
document-> setHtml(strStream);

QPrinter打印机;

QPrintDialog * dialog = new QPrintDialog(& printer,NULL);
if(dialog-> exec()== QDialog :: Accepted){
document-> print(& printer);
}

删除文档;


I am new on QT an I try to print out from QTableView

How can I do this?

Thank a lot

解决方案

Here is a variation of the first answer that gets rid of the intermediate file.

    QString strStream;
    QTextStream out(&strStream);

    const int rowCount = pPublic->tableView->model()->rowCount();
    const int columnCount = pPublic->tableView->model()->columnCount();

    out <<  "<html>\n"
        "<head>\n"
        "<meta Content=\"Text/html; charset=Windows-1251\">\n"
        <<  QString("<title>%1</title>\n").arg(strTitle)
        <<  "</head>\n"
        "<body bgcolor=#ffffff link=#5000A0>\n"
        "<table border=1 cellspacing=0 cellpadding=2>\n";

    // headers
    out << "<thead><tr bgcolor=#f0f0f0>";
    for (int column = 0; column < columnCount; column++)
        if (!pPublic->tableView->isColumnHidden(column))
            out << QString("<th>%1</th>").arg(pPublic->tableView->model()->headerData(column, Qt::Horizontal).toString());
    out << "</tr></thead>\n";

    // data table
    for (int row = 0; row < rowCount; row++) {
        out << "<tr>";
        for (int column = 0; column < columnCount; column++) {
            if (!pPublic->tableView->isColumnHidden(column)) {
                QString data = pPublic->tableView->model()->data(pPublic->tableView->model()->index(row, column)).toString().simplified();
                out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString("&nbsp;"));
            }               
        }
        out << "</tr>\n";
    }
    out <<  "</table>\n"
        "</body>\n"
        "</html>\n";

    QTextDocument *document = new QTextDocument();
    document->setHtml(strStream);

    QPrinter printer;

    QPrintDialog *dialog = new QPrintDialog(&printer, NULL);
    if (dialog->exec() == QDialog::Accepted) {
        document->print(&printer);
    }

    delete document;

这篇关于QTableView打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆