Qt How to implement call printers to print web pages and files in HTML

This article mainly introduces QT how to implement call printers to print the webpage and files in the HTML, the text introduced in the text is very detailed, with a certain reference value, interested partners must have read!

So, in .Pro, QT + = Webkit WebkitWidgets
, I have to print QPrinter,

, so I want to add in .pro QTHAVEMODULE (PRINTSUPPORT): QT + = Printsupport


It is directly attached to the source code, of course, according to some of the experience code and the actual use of some of the online proposal:

#ifndef widget_h # Define widget_h # include #include
 #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  namespace ui {class widget;} class widget: public Qwidget {q_objectpublic: Explicit Widget (Qwidget * Parent = 0 ); ~ Widget (); private slots: void on_pushbutton_clicked (); Void PrintPreview; privatE: UI :: widget * ui; void doprint (); void DoprintPreview (); void createpdf (); void setuppage (); BOOL PrintFile (const qstring & filepath);};    [ Implementation:  
#include “widget.h” #include “ui_widget.h” widget :: widget (Qwidget * Parent): Qwidget (PARENT), UI (New UI: : Widget) {ui-> setupui (this); // There is no use of the qfile file to read the HTML file, because it will make the HTML page Chinese into garbled QString strHtmlText; std :: string bufstr; std :: ifstream readfile; readfile.open “D: \\ qqq.html”); if (readfile.is_open ()) {while (more (readfile, bufstr)) {strHtmlText + = qstring (bufstr.data ());} readfile.close ();} Ui-> WebView-> setHTML (strHtmlText);} widget :: ~ widget () {delete ui;} // Click here to trigger the print action! Void widget :: ON_PUSHBUTTON_CLICKED () {DOPRINT (); // doprintpreview (); // printfile (“d: /123.txt”);} void widget :: DOPRINT () {// Create printer object qprinter printer; / / Create a print dialog QString PrinterName = Printer.printername (); if (Printername.Size () == 0) Return; QPrintDialog DLG (& Printer, this); # if 0 // If there is a zone in the editor, print Select zone if (ui-> textedit-> textcursor (). Have hand () ()) DLG.ADDENABLEDOption (QABSTRACTPRINTDIALOG :: PrintSelection); # ENDIF / / If you press the print button in the dialog box, perform print operation // if (Dlg.exec () == qdialog :: accepted) // Use the default printer here, do not have to select the printer {// ui-> textedit-> print (& printer); ui-> Webview-> Print (& Printer); // print the existing document by absoult path // printfile (“D: /MYRSM.DOC”);}} // Print preview Void widget :: DoprintPreview () {qprinter printer; // Create a print preview dialog QPrintPreviewDialog Preview (& Printer, this); // When you want to generate a preview page, emission a PaintRequested () signal Connect (& Preview, Signal (QPrinter *)), this, slot (PRINTPREVIEW (QPrinter *))); preview.exec ();} void widget :: PrintPreview (qprinter *printer) {// ui-> textedit-> Print (Printer); ui-> WebView-> Print (Printer);} // Generate PDF file void widget :: createpdf () {qstring filename = qfiledialog :: GetSaveFileName (this, TR (“Export PDF File”), qstring (), “* .pdf”); if (! filename.isempty ()) {// If the file suffix is ​​empty, the default uses .pdf if (filename) .Suffix (). ISEMPTY ()) FileName. Append (“. pdf”); qprinter printer; // Specify the output format is pdf printer.setOutputFormat (QPRInter :: pdfformat; printer.setputfilename (filename); // ui-> textedit-> print (& printer); ui-> Webview-> Print (& printer);}} // page Setup 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; // Execute a DOS command adjustment system Print 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;}

How is QT how to achieve call printers Print all the contents of the webpage and files in HTML, thank you for reading! The content that wants to share is helpful to everyone, more related knowledge, welcome to pay attention to Tumi Yun Industry News Channel!
                     
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment