Qt printer print (Solaris 10)

Source: Internet
Author: User
Tags convert text to html drawtext

 

In QT, printing is similar to plotting in qwidget, qpixmap, or qimage. The general steps are as follows: 1 Create the qprinter of the drawing device; 2 The Print dialog box and qprintdialog are displayed. You can select a printer and set attributes; 3 Create a qpainter of qprinter; 4 Use qpainter to draw a page; 5 Call qprinter: Newpage () and draw the next page; 6 , Repeat steps 4 and 5 until all pages are printed. On Windows and Mac OS X platforms, qprinter uses the system's print driverProgram. On UNIX, qprinter generates a script and sends it to LP or LPR (or to a program. The print program has the qprinter function: setprintprogram ()). You can call qprinter: setoutputformat (qprinter: pdfformat) to generate a PDF file. Figure 8.12. Printing Qimage

First, let's look at a simple example to print a qimage to a page. Void printwindow: printimage (const qimage & image) { Qprintdialog printdialog (& printer, this ); If (printdialog.exe C ()){ Qpainter painter (& printer ); Qrect rect = painter. viewport (); Qsize size = image. Size (); Size. Scale (rect. Size (), QT: keepaspectratio ); Painter. setviewport (rect. X (), rect. Y (), Size. Width (), size. Height ()); Painter. setwindow (image. rect ()); Painter. drawimage (0, 0, image ); } } Here, we assume that the printwindow class has a member variable printer of the qprinter type. Of course, we can also create a qprinter on the heap of the printimage () function, but this does not record the settings for printing. Create qprintdialog and call exec () to display it. If you click OK, return true; otherwise, return false. After exec () is called, The qprinter object can be used. (You can also directly call the member function of qprinter for copying without displaying qprintdialog) Then, we create qpainter, and the drawing device is qprinter. Set the window to the rectangle of the displayed image, and draw the image at (0, 0. Generally, the qpainter window is automatically initialized, And the printer and screen have the same resolution (1 inch has 72 to 100 points) to print the control.CodeReusable. In the above function, we set the qpainter window. Printing on one page is simple, but many applications need to print multiple pages. At this time, we print one page at a time, and then call Newpage () to print another page. Here we need to solve the problem of determining how much content is printed on a page. There are two ways to process multi-page printed documents in QT: 1, We can convert data to HTML format and describe them using qtextdocument. qtextdocument is a multi-text engine of QT. 2, Manual Paging Let's take a look at the two methods respectively. In the first example, we want to print a flower guide: one column is the name of the flower, and the other column is the text description. The text format of each entry is stored as "Name: Description ". For example: miltonopsis santanae: a most dangerous orchid species. Since the data of each flower can be represented by a string, we can use qstringlist to represent the data of all flowers. The following code is an example of printing using the QT multi-text engine: Void printwindow: printflowerguide (const qstringlist & entries) { Qstring HTML; Foreach (qstring entry, entries ){ Qstringlist fields = entry. Split (":"); Qstring Title = QT: escape (fields [0]); Qstring body = QT: escape (fields [1]); HTML + = "<Table width =/" 100%/"border = 1 cellspacing = 0>/N" "<Tr> <TD bgcolor =/" lightgray/"> <font size =/" + 1/">" "<B> <I>" + title + "</I> </B> </font>/n <tr> <TD>" + body + "/N </table>/n <br>/N "; } Printhtml (HTML ); } First, convert qstringlist to HTML. Each flower is a line in an HTML table. Call QT: escapte () to convert the special characters '&', '> ', '<' and so on are expressed with the corresponding HTML characters ('amp', '& gt', '& lt'), and then printhtml () is called to print the text: Void printwindow: printhtml (const qstring & HTML) { Qprintdialog printdialog (& printer, this ); If (printdialog.exe C ()){ Qpainter painter (& printer ); Qtextdocument textdocument; Textdocument. sethtml (HTML ); Textdocument. Print (& printer ); } } The printhtml () function pops up the qprintdialog dialog box to print an HTML document. These codes can print arbitrary HTML documents in all QT applications.

Figure 8.13. Printing a flower Guide usingQtextdocument

Currently, it is the most convenient method to convert text to HTML document and print it with qtextdocument. If you need more settings, You need to layout and draw the page by yourself. The following method is to print the flower Guide using manual intervention. First, let's take a look at the printflowerguide () function: Void printwindow: printflowerguide (const qstringlist & entries) { Qprintdialog printdialog (& printer, this ); If (printdialog.exe C ()){ Qpainter painter (& printer ); Qlist <qstringlist> pages; Paginate (& painter, & pages, entries ); Printpages (& painter, pages ); } } After creating a qpainter and setting the printer, call the paginate () function to determine which items are on that page. The result of executing this function is to get a list of qstringlists. Each qstringlist is displayed on a page and the result is passed to printpages () for printing. For example, the flower Guide to be printed has 6 entries: A, B, C, D, E, F. Where A and B are printed on the first page, c, d, e on the second page, and F on the third page. Void printwindow: paginate (qpainter * painter, qlist <qstringlist> * pages, Const qstringlist & entries) { Qstringlist currentpage; Int pageheight = painter-> window (). Height ()-2 * largegap; Int y = 0; Foreach (qstring entry, entries ){ Int Height = entryheight (painter, entry ); If (Y + height> pageheight &&! Currentpage. Empty ()){ Pages-> append (currentpage ); Currentpage. Clear (); Y = 0; } Currentpage. append (entry ); Y + = height + mediumgap; } If (! Currentpage. Empty ()) Pages-> append (currentpage ); } The paginate () function paginate the entries in the huaxiao guide. Calculate the height of each entry based on entryheight. At the same time, consider the vertical distance between the top and bottom of the page largegap. Traverse all entries. If this entry can be placed on the current page, it will be placed in the list of the current page. When the current page is full, place the current page in the page list to start a new page. Int printwindow: entryheight (qpainter * painter, const qstring & entry) { Qstringlist fields = entry. Split (":"); Qstring Title = Fields [0]; Qstring body = Fields [1]; Int textwidth = painter-> window (). Width ()-2 * smallgap; Int maxheight = painter-> window (). Height (); Painter-> setfont (titlefont ); Qrect titlerect = painter-> boundingrect (0, 0, textwidth, maxheight, Qt: textwordwrap, title ); Painter-> setfont (bodyfont ); Qrect bodyrect = painter-> boundingrect (0, 0, textwidth, maxheight, Qt: textwordwrap, body ); Return titlerect. Height () + bodyrect. Height () + 4 * smallgap; } The entryheight () function calculates the vertical distance of each entry based on qpainter: boundingrect (). Figure 8.4 shows the layout of the entry and the meaning of smallgap and mediumgap: The entryheight () function uses qpainter: boundingrect () to compute the vertical space needed by one entry. figure 8.14 shows the layout of a flower entry and the meaning of the smallgap and mediumgap constants. Figure 8.14. A flower entry's layout

Void printwindow: printpages (qpainter * painter,

Const qlist <qstringlist> & pages) { Int firstpage = printer. frompage ()-1; If (firstpage> = pages. Size ()) Return; If (firstpage =-1) Firstpage = 0; Int lastpage = printer. topage ()-1; If (lastpage =-1 | lastpage> = pages. Size ()) Lastpage = pages. Size ()-1; Int numpages = lastpage-firstpage + 1; For (INT I = 0; I <printer. numcopies (); ++ I ){ For (Int J = 0; j <numpages; ++ J ){ If (I! = 0 | j! = 0) Printer. Newpage (); Int index; If (printer. pageorder () = qprinter: firstpagefirst ){ Index = firstpage + J; } Else { Index = lastpage-J; } Printpage (painter, pages [Index], index + 1 ); } } } The function printpages () is used to call printpage () to print each page in order and number of copies. With qprintdialog, you may need to print multiple copies, set the print range, or print them in reverse order. We need to consider these requirements in the program First, determine the print range. Qprinter: frompage () and topage () return the range of pages selected by the user. If no value is selected, 0 is returned. We performed the minus 1 operation because our page index starts from 0. If the user does not select a range, print all pages with firstpage and lastpage content. Then we print each page. The outermost loop is the number of printed copies set by the user. For printers that support multiple copies, qprinter: numcopies () always returns 1. If the printer driver does not support multiple copies, numcopies () returns the number of copies specified by the user, and multiple copies can be printed by an application. (In the qimage example in this section, we did not consider printing multiple copies for the sake of simplicity .) Figure 8.15. Printing a flower Guide using Qpainter The number of printed pages cyclically in the inner layer. If the page number is not the first page, call Newpage () to clear the original page and start filling the new page. Call printpage () to print each page. Void printwindow: printpage (qpainter * painter, Const qstringlist & entries, int pagenumber) { Painter-> Save (); Painter-> translate (0, largegap ); Foreach (qstring entry, entries ){ Qstringlist fields = entry. Split (":"); Qstring Title = Fields [0]; Qstring body = Fields [1]; Printbox (painter, title, titlefont, QT: lightgray ); Printbox (painter, body, bodyfont, QT: White ); Painter-> translate (0, mediumgap ); } Painter-> restore (); Painter-> setfont (footerfont ); Painter-> drawtext (painter-> window (), Qt: alignhcenter | QT: alignbottom, Qstring: Number (pagenumber )); } The function printpage () prints every entry in the page. Print the title with printbox (), and then print the description with printbox. Print the page number at the bottom of each page. Figure 8.16. The flower guide's page layout Void printwindow: printbox (qpainter * painter, const qstring & STR, Const qfont & font, const qbrush & brush) { Painter-> setfont (font ); Int boxwidth = painter-> window (). Width (); Int textwidth = boxwidth-2 * smallgap; Int maxheight = painter-> window (). Height (); Qrect textrect = painter-> boundingrect (smallgap, smallgap, Textwidth, maxheight, Qt: textwordwrap, STR ); Int boxheight = textrect. Height () + 2 * smallgap; Painter-> setpen (qpen (QT: Black, 2, QT: solidline )); Painter-> setbrush (Brush ); Painter-> drawrect (0, 0, boxwidth, boxheight ); Painter-> drawtext (textrect, QT: textwordwrap, STR ); Painter-> translate (0, boxheight ); } Printbox () First, draw a rectangle box, and then draw text in the rectangle box. Reprinted statement:This article is transferred from Http://blog.csdn.net/iamdbl/archive/2007/10/08/1816001.aspx Knowledge Development: Qt Reference document: http://www.kuqin.com/qtdocument/index.html

 

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.