Home >Web Front-end >HTML Tutorial >How does QT call the printer to print web pages and files in html?
这篇文章介绍的内容是QT如何实现调用打印机来打印html中的网页及文件,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
工程里用到 QWebView,
所以要在.pro里加上 QT += webkit webkitwidgets
打印用到了 QPrinter,
所以要在.pro里加上 qtHaveModule(printsupport): QT += printsupport
直接附上源码吧,当然也是根据网上一些前辈的经验代码和实际使用情况改进而来:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QTableWidget> #include <QPainter> #include <QtPrintSupport/QPrinter> #include <QtPrintSupport/QPrintDialog> #include <QFileInfo> #include <windows.h> #include <QFileDialog> #include <QtPrintSupport/QPrintPreviewDialog> #include <QtPrintSupport/QPageSetupDialog> #include <fstream> #include <iostream> #include <sstream> #include <QWebView> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void on_pushButton_clicked(); void printPreview(QPrinter *printer); private: Ui::Widget *ui; void doPrint(); void doPrintPreview(); void createPdf(); void setUpPage(); bool printFile(const QString & filePath); };
实现:
#include "Widget.h" #include "ui_Widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); //这里没用QFile文件读html文件,因为会使html网页中文成乱码 QString strHtmltext; std::string bufStr; std::ifstream readFile; readFile.open("D:\\qqq.html"); if(readFile.is_open()) { while(getline(readFile, bufStr)) { strHtmltext += QString(bufStr.data()); } readFile.close(); } ui->webView->setHtml(strHtmltext); } Widget::~Widget() { delete ui; }//点击这里触发打印动作! void Widget::on_pushButton_clicked() { doPrint(); // doPrintPreview(); // printFile("D:/123.txt"); } void Widget::doPrint() { // 创建打印机对象 QPrinter printer; // 创建打印对话框 QString printerName = printer.printerName(); if( printerName.size() == 0) return; QPrintDialog dlg(&printer, this);#if 0 //如果编辑器中有选中区域,则打印选中区域 if (ui->textEdit->textCursor().hasSelection()) dlg.addEnabledOption(QAbstractPrintDialog::PrintSelection); #endif // 如果在对话框中按下了打印按钮,则执行打印操作 // if (dlg.exec() == QDialog::Accepted) //这里使用默认打印机,不用每次选择打印机 { // ui->textEdit->print(&printer); ui->webView->print(&printer); // print the existing document by absoult path // printFile("D:/myRSM.doc"); } } // 打印预览void Widget::doPrintPreview() { QPrinter printer; // 创建打印预览对话框 QPrintPreviewDialog preview(&printer, this); // 当要生成预览页面时,发射paintRequested()信号 connect(&preview, SIGNAL(paintRequested(QPrinter*)),this,SLOT(printPreview(QPrinter*))); preview.exec(); } void Widget::printPreview(QPrinter *printer) { // ui->textEdit->print(printer); ui->webView->print(printer); } // 生成PDF文件 void Widget::createPdf() { QString fileName = QFileDialog::getSaveFileName(this, tr("导出PDF文件"), QString(), "*.pdf"); if (!fileName.isEmpty()) { // 如果文件后缀为空,则默认使用.pdf if (QFileInfo(fileName).suffix().isEmpty()) fileName.append(".pdf"); QPrinter printer; // 指定输出格式为pdf printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName(fileName); // ui->textEdit->print(&printer); ui->webView->print(&printer); } } // 页面设置void Widget::setUpPage() { QPrinter printer; QPageSetupDialog pageSetUpdlg(&printer, this); if (pageSetUpdlg.exec() == QDialog::Accepted) { printer.setOrientation(QPrinter::Landscape); } else { printer.setOrientation(QPrinter::Portrait); } } bool Widget::printFile(const QString & filePath) { // create a printer QPrinter printer; QString printerName = printer.printerName(); // no default printer or no file return false if( printerName.size() == 0 || filePath.size() == 0) return false; QFileInfo fi(filePath); if(!fi.exists()) return false; int ret = 0; //执行DOS命令调系统打印 ret = (int)ShellExecuteW(NULL, QString("print").toStdWString().c_str(), filePath.toStdWString().c_str(), NULL, NULL, SW_HIDE); // if return number bigger than 31 indicate succ if (ret > SE_ERR_NOASSOC) return true; return false; }
相关推荐:
The above is the detailed content of How does QT call the printer to print web pages and files in html?. For more information, please follow other related articles on the PHP Chinese website!