On the realization of QT printing function

Source: Internet
Author: User

As a lightweight, integrated development environment, QT is designed to enable developers to develop applications faster and easier with QT, the application framework. To achieve this, QT must be able to cross-platform and QT can run on both 32-bit and 64-bit LINUX,MAC OS x and Windows.

Now we want to add a print function to the project, even in different operating system environments, can still have the same user experience, in the implementation of the discovery QT printing support is not so friendly. We want to be able to print a variety of documents, such as a user's CV, in a cross-platform scenario, and find it extremely difficult to do this without invoking the kernel shell print function of the corresponding operating system. We want to use QT's own function library, to achieve a simple printing function, so that the implementation of a code, multi-environment use purposes.

Here are two methods that we'll cover separately.

First, call the kernel shell printing function

Linux is relatively easy to use, using lpr–p [printer] [fileName], that is, if you want to send file.cpp files to the printer pl use lpr–p pl file.cpp.

Under Windows, we implement the print function by invoking the Windows Kernel command execution function when we are implementing a document that prints the established file path. The specific code is as follows:

BOOL 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;    ret = (int) shellexecutew (NULL,                  QString ("print"). Tostdwstring (). C_STR (),                  filepath.tostdwstring (). C_STR ( ),                  null,                  null,                  sw_hide);    If return number bigger than to indicate SUCC    if (Ret > Se_err_noassoc)        return true;    return false;}

Second, the use of QT custom classes and functions to achieve the function of printing files

ClassName function
Qabstractprintdialog Base implementation for print dialogs used to configure printers
Qpagesetupdialog Configuration dialog for the page-related options on a printer
Qprintdialog Dialog for specifying the printer ' s configuration
Qprintengine Defines an interface for what Qprinter interacts with a given printing subsystem
Qprintpreviewdialog Dialog for previewing and configuring page layouts for printer output
Qprintpreviewwidget Widget for previewing page layouts for printer output
Qprinter Paint device that paints on a printer
Qprinterinfo Gives access to information about existing printers

The classes associated with QT and print files are as follows:

    • Abstract Document Printing dialog box class, providing the basic implementation of the Print dialog box for configuring the printer;
    • The Page Setup dialog box class, and the print page related parameters configuration dialog;
    • Print dialog box class, specify the printer configuration dialog box;
    • The Print Engine class, which defines the interface of how the Qprinter class interacts with the print subsystem;
    • The print Preview dialog class, which is used to preview and configure dialog boxes for page layouts;
    • Print Preview control class, preview the page layout of the control;
    • Printer class that indicates how the printer works.

How to implement printing in QT

In QT If you want to implement printing, first, we need to construct a printer class (Qprinter) object, and secondly, use this object to construct a print dialog Class (Qprinterdialog) object. As shown below:

Qprinter printer; Qprinterdialog *dialog = new Qprintdialog (&printer, this);d Ialog->setwindowtitle (tr ("Print Document")); if ( Editor->textcursor (). Hasselection ()) dialog->addenabledoption (qabstractprintdialog::P rintselection); if ( Dialog->exec ()! = qdialog::accepted) return;

Finally attached to the project, how can we use Qt's own class to achieve this cross-platform printing function.

First, specify the slot function in the header file:

Private Slots:void doprint ();    void DoPrintPreview ();    void PrintPreview (Qprinter *printer);    void CreatePDF (); void Setuppage (), specifically implemented as follows: void MainWindow::d oprint () {//Create printer object Qprinter printer;    Create Print dialog box QString printername = Printer.printername ();    if (printername.size () = = 0) return;    Qprintdialog Dlg (&printer, this); If there is a selected area in the editor, print the selected area if (Ui->textedit->textcursor (). Hasselection ()) Dlg.addenabledoption (qabstractprintdi    Alog::P rintselection);       If the Print button is pressed in the dialog box, the print operation is performed if (dlg.exec () = = qdialog::accepted) {ui->textedit->print (&printer);    Print the existing document by Absoult Path//Printfile ("D:/myrsm.doc");    }}//print preview void MainWindow::d oprintpreview () {Qprinter printer;    Create Print Preview dialog box Qprintpreviewdialog Preview (&printer, this); When you want to generate a preview page, launch the paintrequested () signal Connect (&preview, SIGNAL (paintrequested (qprinter*)), This,slot (p Rintpreview (qprinter*))); Preview.exec ();} void MainWindow::p rintpreview (Qprinter *printer) {ui->textedit->print (printer);} Generate PDF file void Mainwindow::createpdf () {QString fileName = Qfiledialog::getsavefilename (This, TR ("Export PDF file"), QString ()    , "*.pdf"); if (!filename.isempty ()) {//If the file suffix is empty, the. pdf if (Qfileinfo (fileName). suffix (). IsEmpty ()) file is used by default.        Name.append (". pdf");        Qprinter printer;        Specifies the output format as PDF Printer.setoutputformat (qprinter::P dfformat);        Printer.setoutputfilename (FileName);    Ui->textedit->print (&printer);    }}//page set void Mainwindow::setuppage () {Qprinter printer;    Qpagesetupdialog Pagesetupdlg (&printer, this); if (pagesetupdlg.exec () = = qdialog::accepted) {printer.setorientation (qprinter::landscape);} Else{printer.setorientation (qprinter::P ortrait);}}

Above, only for the experience of a little bit of QT, for everyone to share the practice, the shortcomings are welcome to point out.

Http://express.ruanko.com/ruanko-express_75/tech-overnight1.html

http://blog.csdn.net/jdh99/article/details/42585987

On the realization of QT printing function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.