原地址: http://www.cppblog.com/biao/archive/2009/09/08/95603.html
QString htmlString = QString("<html>") + str1 + str2 +str3 + finerysInfo + str5 + str6 + QString("<br>%1<br>%2").arg(separateString) + QString("</html>"); printString(htmlString); accept(); void PrintDialog::printString(const QString &htmlString) { qDebug() << htmlString; QPrinter p; QTextDocument doc; //doc.setHtml(htmlString); QFont font = doc.defaultFont(); font.setBold(true); font.setPointSize(font.pointSize() + 1); doc.setDefaultFont(font); QSizeF s = QSizeF(p.logicalDpiX() * (58 / 25.4), p.logicalDpiY() * (297 / 25.4)); doc.setPageSize(s); p.setOutputFormat(QPrinter::NativeFormat); // 打N页 int pageCount = ui->pageCountLineEdit->text().toInt(); for (int i = 1; i <= pageCount; ++i) { // 如果只打印一份,即只打印店联 if (1 == pageCount) { if (Receive_Finery == operationType) { doc.setHtml(htmlString.arg(tr("店联"))); } else { doc.setHtml(htmlString.arg(tr("客户联"))); } } else { if (i % 2 != 0) { doc.setHtml(htmlString.arg(tr("客户联"))); } else { doc.setHtml(htmlString.arg(tr("店联"))); } } doc.print(&p); if (i < pageCount) { p.newPage(); } } }