index.lxp@lxpwrap=x2765_252ehtm.htm

来自「GUI Programming with Python」· HTM 代码 · 共 765 行 · 第 1/2 页

HTM
765
字号
CLASS="CLASSNAME">QImageIO</TT>        class), and for manipulating the actual pixels of an image.        There's another image-related class,        <TTCLASS="CLASSNAME">QPicture</TT>, which can be used to record        drawing operations and replay them later. The recorded paint        events can then be stored in a file and reloaded later on.        Those files are called <SPAN><ICLASS="EMPHASIS">meta-files</I></SPAN> &#8212;        but they're in a special Qt format. In Qt 3,        <TTCLASS="CLASSNAME">QPicture</TT> also supports the standard        scalable vector graphics format, svg. If you want to create a        complex vector-drawing application you'd be well advised to        stick to this standard.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QPainter</A></H2><P>A <TTCLASS="CLASSNAME">QPainter</TT> object        is used to efficiently paint on any paintdevice using a        variety of primitive graphics, such as simple dots or lines,        bezier curves, polygons, strings of text (using a particular        font) or pixmaps. Drawings can be modified, for instance by        shearing or rotating, and parts can be erased or clipped. We        have already used a QPainter to draw the scribbly lines in        previous examples.        </P><P>Paint devices can be:</P><P></P><UL><LI><P>pictures:              <TTCLASS="CLASSNAME">QPicture</TT></P></LI><LI><P>pixmaps:              <TTCLASS="CLASSNAME">QPixmap</TT></P></LI><LI><P>printers:              <TTCLASS="CLASSNAME">QPrinter</TT></P></LI><LI><P>widgets <TTCLASS="CLASSNAME">: QWidget</TT> (and              all children of <TTCLASS="CLASSNAME">QWidget</TT>)            </P></LI></UL><P>What can be drawn on one device, can        be drawn on all devices, so it's uncommonly easy to print on        paper what can be drawn on screen. Copying batches of pixels        from one paint device to another is blindingly fast if you use        the bitBlt global function, as we did above for our        double-buffered graphics editor.</P><P>Note that you cannot create        <SPAN><ICLASS="EMPHASIS">any</I></SPAN> paint device until you have created a        <TTCLASS="CLASSNAME">QApplication</TT>. This includes        <TTCLASS="CLASSNAME">QPixmap</TT>s. The following variant on        action.py won't work, even though it seems a        <SPAN><ICLASS="EMPHASIS">good</I></SPAN> idea to pre-create the pixmap,        instead of converting the xpm data on constructing the        <TTCLASS="CLASSNAME">QAction</TT>:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 10-6. fragment from action2.py - You cannot create a            QPixmap before a QApplication</B></P><PRECLASS="PROGRAMLISTING">## action2.py#import sysfrom qt import *connectIcon=QPixmap(["16 14 5 1",             " 	c None",             ".	c black",             "X	c gray50",             "o	c red",             "O	c yellow",             "                ",             "          .     ",             "       X .X     ",             "      XooX  .   ",             "     Xoooo .X   ",             "    XooooooX    ",             "    XooooooX    ",             "    XoooooX.    ",             "    XooooX.     ",             "   XOXXXX.      ",             "  XOXX...       ",             " XOXX           ",             "  XX            ",             "  X             "             ])class MainWindow(QMainWindow):    def __init__(self, *args):        apply(QMainWindow.__init__, (self, ) + args)...        self.action.setIconSet(QIconSet(connectIcon))...        </PRE></DIV><P>Running this gives the following result:</P><PRECLASS="SCREEN">boudewijn@maldar:~ &#62; python action2.pyQPaintDevice: Must construct a QApplication before a QPaintDeviceAborted      </PRE><P><A href="index.lxp@lxpwrap=c7391_252ehtm.htm">Chapter 21</A> deals with painters and paintdevices        in quite a lot of detail, while <A href="index.lxp@lxpwrap=c8100_252ehtm.htm">Chapter 24</A> deals        with printing to paper.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QFont</A></H2><P>There is no other area where there are so        many and profound differences between operating systems as        there is with fonts. And if you take into account the        difference in font handling between printers and screens, you        will get a feeling for how difficult it is to get proper and        dependable cross-platform multi-lingual font support in a        toolkit.        </P><P>Fortunately, Qt's font support has        steadily improved, and is now at the point where, provided        good quality fonts are available on a system, it can offer the        same excellent screen <SPAN><ICLASS="EMPHASIS">and</I></SPAN> printer support        on all platforms.</P><P>The first issue is the font used for        drawing labels and other application texts &#8212; sometimes        called the system font. This naturally differs for each        system: Windows uses Arial these days, while KDE uses        Helvetica, CDE Times and OS X a bold Helvetica. Furthermore,        the system font is also often customized by the user. Text in        one font takes more room than text in another font &#8212;        possibly giving ugly display errors. By using Qt's layout        managers, instead of positioning widgets with pixel-precision        yourself, you will have little trouble dealing with the        geometry differences between Window's Arial font and KDE's        Helvetica standard &#8212; all controls will reflow        neatly.</P><P>For handling fonts in your application        you can work with <TTCLASS="CLASSNAME">QFont</TT>. Qt builds its        own database of available fonts from whatever the system        provides. You can then access these fonts in a        system-independent manner, without having to juggle X11 font        resource names yourself.</P><P><TTCLASS="CLASSNAME">QFont</TT> provides all        necessary functions to select encodings (or scripts in Qt3),        font families, styles and sizes. There's also a standard        dialog available, <TTCLASS="CLASSNAME">QFontDialog</TT> that you        can use to let the user select a certain font.</P><P>There are serious differences between the        font system in Qt2 and Qt3. In Qt2, you need to determine        which character set encoding you need; and you can only use        the character set encodings that the particular font supports.        For instance, if your font supports the KOI8 Cyrillic        encoding, then that is the encoding you can use. The font you        request has a one-to-one relation with the font files on your        system.</P><P>In Qt3, you select fonts by name, style        and script (like Cyrillic), and Qt will select the closest        fitting font. If your widget needs to present text on screen        that uses characters that cannot be retrieved from the        selected font, Qt will query all other fonts on your system,        and assemble a composite, synthetic font that includes all        characters you need. You lose some control but you gain a        correct representation of all possible texts&#8212; you can        use any font for any text in any script.</P><P>If you want to set a certain font for the        entire application, you can use the        <TTCLASS="FUNCTION">QApplication.setFont</TT> class function.        Likewise, everything that descends from        <TTCLASS="CLASSNAME">QWidget</TT> also has a        <TTCLASS="FUNCTION">setFont()</TT> function.</P><P>You can use <TTCLASS="CLASSNAME">QFontInfo</TT> to        determine the exact font Qt uses for a certain QFont &#8212; but        this might be quite slow. An important use of        <TTCLASS="CLASSNAME">QFontInfo</TT> with Qt3 is to determine        whether the font you get was exactly the font you asked for.        For instance, if you desire a Bembo font, which might not be        present on your system, you could get something closeish: a        Times New Roman. Especially for drawing and dtp applications        it's important to be sure which font is actually used.</P><P><TTCLASS="CLASSNAME">QFontMetrics</TT> can        be used to determine metrics information about a font. For        instance, how high the ascenders and descenders are, and how        wide the widest character is. This is useful if you need to        determine how much space a line of text takes when printed on        paper. </P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Font metrics</P></DIV></P></DIV></DIV></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=x2693_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=x2976_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Application classes</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c2591_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Basic widgets</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?