📄 index.lxp@lxpwrap=x8148_252ehtm.htm
字号:
<table border="0" cellspacing="0" cellpadding="3" width="100%"><tr><td> <div align="center" id="bldcontent"> <a href="../default.htm"><img src="../images/opendocs.png" width="63" height="76" border="0"></a> <br> <div class="symbol">Your OpenSource Publisher™</div> </div> </td></tr></table> <div align="center" class="author"> <a href="../products.lxp">Products</a> | <a href="../wheretobuy.lxp">Where to buy</a> | <a href="../bookstore.lxp">Retailers</a> | <a href="../faq.lxp">FAQ</a> | <a href="../writeforus.lxp">Write for Us.</a> | <a href="#contact">Contact Us.</a> </div> <table border="0" cellspacing="3" cellpadding="0" width="100%"><tr><td width="100%"> <div class="content"> <table border="0" cellspacing="2" cellpadding="0" width="100%"><tr><td width="100%"> <div align="center"><H4 CLASS="AUTHOR"><A NAME="AEN5">Boudewijn Rempt</A><br><a href="../../https@secure.linuxports.com/opendocs/default.htm"><img src=odpyqt125.png></a><br>ISBN: 0-97003300-4-4<br><a href="../../https@secure.linuxports.com/opendocs/default.htm">Available from bookstores everywhere or you can order it here.</a><p>You can download the source files for the book <a href="pyqtsrc.tgz">(code / eps) here.</a><hr></div> <HTML><HEAD><TITLE>Putting ink to paper</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.72"><LINKREL="HOME"TITLE="GUI Programming with Python: QT Edition"HREF="book1.htm"><LINKREL="UP"TITLE="Printing"HREF="c8100.htm"><LINKREL="PREVIOUS"TITLE="Adding printing to Kalam"HREF="x8141.htm"><LINKREL="NEXT"TITLE="Conclusion"HREF="x8207.htm"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GUI Programming with Python: QT Edition</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><A accesskey="P" href="index.lxp@lxpwrap=x8141_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 24. Printing</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x8207_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Putting ink to paper</A></H1><P>The following is a very simplistic way of dumping text onto paper. We merely run through all lines in the active view and dump them on paper. If a line is too long for the chosen paper-size, we'll just let it run of the paper, we won't implement word-wrap.</P><PRECLASS="PROGRAMLISTING"> def slotFilePrint(self): margin = 10 pageNo = 1 if self.printer.setup(self): self.statusBar().message('Printing...') view = self.workspace.activeWindow() p = QPainter(self.printer) p.setFont(QFont("courier", 10)) y = 0 fm = p.fontMetrics() metrics = QPaintDeviceMetrics(self.printer) for i in range(view.numLines()): if margin + y > metrics.height() - margin: pageNo = pageNo + 1 self.printer.newPage() y = 0 p.drawText(margin, margin + y, metrics.width(), fm.lineSpacing(), Qt.ExpandTabs | Qt.DontClip, view.textLine(i)) y = y + fm.lineSpacing() self.statusBar().message('Printing completed',2000) else: self.statusBar().message('Printing aborted',2000) </PRE><P>You can see how printing text works. A <TTCLASS="CLASSNAME">QPrinter</TT> object is a paint device, so we create a <TTCLASS="CLASSNAME">QPainter</TT> for it.</P><P>Printing requires choosing a font. In all probability, the user's screen font is not suitable for printing. For instance, many people read text on screen in 12 or 14 point fonts, but prefer printing with 10 point fonts. In the preceding code, a ten-point courier is chosen, though ideally you would want the choice of printing font to be part of the application settings.</P><P>Once the font is set, we can use <TTCLASS="FUNCTION">QPainter.fontMetrics()</TT> to retrieve the height that each line will take on the paper. If the top margin (<TTCLASS="VARNAME">margin</TT>) plus the current line position (<TTCLASS="VARNAME">y</TT>) is greater than the height of the page, it's time for a new page. The page height is retrieved with <TTCLASS="FUNCTION">metrics.height()</TT> which uses <TTCLASS="CLASSNAME">QPaintDeviceMetrics</TT> to provide this kind of practical information.</P><P>Actually printing each line is no different from painting text with a <TTCLASS="CLASSNAME">QPainter</TT> on a <TTCLASS="CLASSNAME">QPaintDevice</TT>. The <TTCLASS="FUNCTION">drawText()</TT> paints the text on the device. You have to compute the x and y position, width and height of the area covered by the text to determine where exactly the text is printed.</P><P>These are <TTCLASS="VARNAME">Qt.AlignmentFlags</TT>, so you can mix and match <TTCLASS="VARNAME">AlignLeft</TT>, <TTCLASS="VARNAME">AlignRight</TT>, <TTCLASS="VARNAME">AlignHCenter</TT>, <TTCLASS="VARNAME">AlignTop</TT>, <TTCLASS="VARNAME">AlignBottom</TT>, <TTCLASS="VARNAME">AlignVCenter</TT>, <TTCLASS="VARNAME">AlignCenter</TT>, <TTCLASS="VARNAME">SingleLine</TT>, <TTCLASS="VARNAME">DontClip</TT>, <TTCLASS="VARNAME">ExpandTabs</TT>, <TTCLASS="VARNAME">ShowPrefix</TT>, <TTCLASS="VARNAME">WordBreak</TT>. In this case, <TTCLASS="VARNAME">ExpandTabs</TT> is used to make sure any tabs in the text are neatly printed, and <TTCLASS="VARNAME">DontClip</TT> is used to prevent errors when a line is too long for the page.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><A accesskey="P" href="index.lxp@lxpwrap=x8141_252ehtm.htm">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="H" href="index.lxp@lxpwrap=book1_252ehtm">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><A accesskey="N" href="index.lxp@lxpwrap=x8207_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Adding printing to Kalam</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c8100_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Conclusion</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -