Get error messages when using QPrinter, QPainter and QTextDocument together



  • In my Qt application, I want to create a preview page with the content that contains the Header, Footer title and a TableView.

    This is the code I used:

    @
    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 << "&lt;html&gt;\n"
    "&lt;head&gt;\n"
      "&lt;meta content=\"text/html; charset=utf-8\"&gt;\n"
      "&lt;title&gt;Demo MyTableView&lt;/title&gt;\n"
      "&lt;style tyle=\"text/css\"&gt;th{font-size: 14pt}\n td{font-size: 12pt}\n table td + td + td + td{font-weight:bold}&lt;/style&gt;"
     "&lt;/head&gt;\n"
     "&lt;body bgcolor=#ffffff link=#5000A0&gt;\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);
    

    }
    @

    When I run the application, I only see Header and Footer title in the preview page, the TableView is not shown. Then I used qDebug() to check and I got the error messages

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

    at the line

    @document->print(printer); @

    How can I solve this issue to print the data normally with the Header, Footer title and TableView?

    Thanks for your help!



  • The painter is already using the printer, so you can't use it until you end the painting with it. I would try it with drawStaticText. QStaticText should be able to draw html.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.