简体   繁体   中英

QPrinter Print Table And Text

I am trying to show on the same page a QTextTable and some text I am creating using QPainter. When i have the QTextTable above the QPainter the only thing that prints is the table but when i have the QPainter above the QTextTable there is nothing not even the page number. How can I show a QTextTable and text at the same time?

QPrinter Print(QPrinter::HighResolution);
Print.setPaperSize(QPrinter::A4);
Print.setOrientation(QPrinter::Landscape);
Print.setDuplex(QPrinter::DuplexAuto);
Print.setOrientation(QPrinter::Portrait);
Print.newPage();
QPrintDialog Printdialog(&Print);
Printdialog.setWindowTitle("Print");
Printdialog.exec();
QPainter painter;
QRect rect(0,0,250,250);
painter.drawText(100, 100, "Test");
painter.begin(&Print);
QTextCursor cursor(ui->textBrowser->textCursor());
cursor.movePosition(QTextCursor::Start);
QTextTable *table = cursor.insertTable(ui->tableWidget_Invoice->rowCount() +1, 5);
table->cellAt(0,0).firstCursorPosition().insertText("Product Code");
table->cellAt(0,1).firstCursorPosition().insertText("Product Description");
table->cellAt(0,2).firstCursorPosition().insertText("Qty");
table->cellAt(0,3).firstCursorPosition().insertText("Unit Price");
table->cellAt(0,4).firstCursorPosition().insertText("Total Price");
for(int x = 0; x<ui->tableWidget_Invoice->rowCount(); x++) //Insert Contents of Form Table Into Printout Table
{
    table->cellAt(x+1,0).firstCursorPosition().insertText(ui->tableWidget_Invoice->item(x,2)->text());
    table->cellAt(x+1,1).firstCursorPosition().insertText(ui->tableWidget_Invoice->item(x,1)->text());
    table->cellAt(x+1,2).firstCursorPosition().insertText(ui->tableWidget_Invoice->item(x,0)->text());
    table->cellAt(x+1,3).firstCursorPosition().insertText(ui->tableWidget_Invoice->item(x,3)->text());
    table->cellAt(x+1,4).firstCursorPosition().insertText(ui->tableWidget_Invoice->item(x,4)->text());

}
ui->textBrowser->print(&Print);

What you will have to do is not print from the widget, you will have to render it to a Qpainter. the code you will use is below.

double xscale = Print.pageRect().width()/double(ui->textBrowser->width());
double yscale = Print.pageRect().height()/double(ui->textBrowser->height());
double scale = qMin(xscale, yscale);
painter.translate(Print.paperRect().x() + Print.pageRect().width()/2,
                           Print.paperRect().y() + Print.pageRect().height()/2);
painter.scale(scale, scale);
painter.translate(-width()/2, -height()/2);
painter.drawText(10,10, "TEST");
ui->textBrowser->render(&painter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM