File file1(htmlFileName1);if (!file.open(QFile::ReadOnly)) return false;QFile file2(htmlFileName2);if (!file2.open(QFile::ReadOnly)) return false; QTextEdit textEdit(0);QByteArray data = file1.readAll();QTextCodec *codec = Qt::codecForHtml(data);QString str = codec->toUnicode(data);textEdit.setHtml(str);data = file2.readAll();codec = Qt::codecForHtml(data);str = codec->toUnicode(data); textEdit.append(str);
QPrinter printer(QPrinter::HighResolution);QPrintPreviewDialog prv(&printer, &textEdit);connect(&prv, SIGNAL(paintRequested(QPrinter *)), &textEdit, SLOT(print(QPrinter *)));
class CQTextEdit : public QTextEdit{Q_OBJECTpublic: CQTextEdit(QWidget* parent): QTextEdit(parent) {}private slots: void print(QPrinter *);};void CQTextEdit::print(QPrinter* p){ QTextEdit::print(p);}СQTextEdit textEdit(0);....QPrinter printer(QPrinter::HighResolution);QPrintPreviewDialog prv(&printer, &textEdit);connect(&prv, SIGNAL(paintRequested(QPrinter *)), &textEdit, SLOT(print(QPrinter *)));
C++ (Qt)class CQTextEdit : public QTextEdit{Q_OBJECTpublic: CQTextEdit(QWidget* parent): QTextEdit(parent) {} private slots: void print(QPrinter *);}; void CQTextEdit::print(QPrinter* p){ QTextEdit::print(p);} void MainWindow::on_testButton_clicked(){ CQTextEdit* TextEdit = new CQTextEdit(this); TextEdit->setHtml(...); QPrinter printer(QPrinter::HighResolution); QPrintPreviewDialog prv(&printer); QObject::connect(&prv, SIGNAL(paintRequested(QPrinter *)), TextEdit, SLOT(print(QPrinter *))); prv.exec();}
C++ (Qt)class CQTextEdit : public QTextEdit{Q_OBJECTpublic: CQTextEdit(QWidget* parent): QTextEdit(parent) {} private slots: void print(QPrinter *);}; void CQTextEdit::print(QPrinter* p){ QTextEdit::print(p);} void MainWindow::on_testButton_clicked(){ CQTextEdit* textEdit = new CQTextEdit(this); QTextBlockFormat blockFmt; blockFmt.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore); QTextCursor cur(textEdit->document()); cur.insertHtml("<b> First Page </b>"); cur.insertBlock(blockFmt); cur.insertHtml("<b> Second Page </b>"); cur.insertBlock(blockFmt); cur.insertHtml("<b> Third Page </b>"); QPrinter printer(QPrinter::HighResolution); QPrintPreviewDialog prv(&printer); QObject::connect(&prv, SIGNAL(paintRequested(QPrinter *)), textEdit, SLOT(print(QPrinter *))); prv.exec();}