0

在我的 Qt 应用程序中,我想创建一个预览页面,其内容包含 Header、Footer 标题和 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);
}

当我运行应用程序时,我只在预览页面中看到 Header 和 Footer 标题,没有显示 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); 

如何解决此问题以正常打印带有 Header、Footer 标题和 TableView 的数据?

谢谢你的帮助!

4

1 回答 1

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);
    [..]
}
于 2013-10-29T10:01:20.420 回答