一起使用QPrinter,QPainter和QTextDocument时获取错误消息

时间:2013-10-29 09:47:56

标签: qt qpainter qtextdocument qprinter

在我的Qt应用程序中,我想创建一个包含页眉,页脚标题和TableView的内容的预览页面。

这是我使用的代码:

void MainWindow::print(QPrinter *printer)
{
    int xscale = 50;
    int yscale = 30;

    QPoint top_left = QPoint(xscale, yscale);
    QPoint top_right = QPoint(xscale + 552, yscale + 20);
    QPoint bottom_left = QPoint(xscale, yscale + 1020);
    QPoint bottom_right = QPoint(xscale + 492, yscale + 1020);

    QPainter painter(printer);
    painter.setRenderHints(QPainter::Antialiasing |
                       QPainter::TextAntialiasing |
                       QPainter::SmoothPixmapTransform, true);

    // Header
    painter.setFont(QFont("Arial", 10));
    painter.drawImage(top_left, QImage(":/images/images/logo.png"));
    painter.drawText(top_right, "Header");

    // Print the Table
    QString strStream;
    QTextStream out(&strStream);

    out << "<html>\n"
            "<head>\n"
                 "<meta content=\"text/html; charset=utf-8\">\n"
                 "<title>Demo MyTableView</title>\n"
                 "<style tyle=\"text/css\">th{font-size: 14pt}\n td{font-size: 12pt}\n table td + td + td + td{font-weight:bold}</style>"
             "</head>\n"
             "<body bgcolor=#ffffff link=#5000A0>\n"
                 "<table cellspacing=\"0\" cellpadding=\"2\" border=\"1\" width=\"100%\">\n";

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

    // Print the data
    for (int row = 0; row < rowCount; row++) {
        out << "<tr>";
        for (int column = 0; column < columnCount; column++) {
            if (!myTableView->isColumnHidden(column)) {
                QString data = myTableView->model()->data(myTableView->model()->index(row, column)).toString().simplified();
                out << QString("<td bkcolor=0 align=center>%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);
    document->print(printer);  // I got the error messages at here
    delete document;

    // Footer
    painter.setFont(QFont("Arial", 10));
    painter.drawText(bottom_left, "Copyright 2013");

    // Get current date and time
    QDateTime dateTime = QDateTime::currentDateTime();
    QString dateTimeString = dateTime.toString();
    painter.drawText(bottom_right, dateTimeString);
}

当我运行应用程序时,我只在预览页面中看到页眉和页脚标题,而不显示TableView。然后我使用qDebug()来检查并收到错误消息

QPrinter::setDocName: Cannot be changed while printer is active
QPainter::begin: A paint device can only be painted by one painter at a time.

document->print(printer); 

如何使用页眉,页脚标题和TableView正常打印数据?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好吧,如果Qt抱怨一次使用多个画家,只允许使用一个:)换句话说,只需将MainWindow::print()函数中的代码拆分为文档每个部分的较小例程:

void MainWindow::drawHeader(QPrinter *printer)
{
    QPainter painter(printer);
    // .. Draw the header
    [..]
}

void MainWindow::drawFooter(QPrinter *printer)
{
    QPainter painter(printer);
    painter.setFont(QFont("Arial", 10));
    painter.drawText(bottom_left, "Copyright 2013");
    [..]
}

void MainWindow::drawTable(QPrinter *printer)
{
    QTextDocument document;
    document.print(printer);
    [..]
}

最后:

void MainWindow::print(QPrinter *printer)
{
    // Init something.
    drawHeader(printer);
    drawTable(printer);
    drawFooter(printer);
    [..]
}