index.lxp@lxpwrap=x2629_252ehtm.htm

来自「GUI Programming with Python」· HTM 代码 · 共 336 行

HTM
336
字号
    <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&#153;</div>    </div>      </td></tr></table>    <div align="center" class="author">      	<a href="../products.lxp">Products</a>	&nbsp;|&nbsp;	<a href="../wheretobuy.lxp">Where to buy</a>	&nbsp;|&nbsp;	<a href="../bookstore.lxp">Retailers</a>	&nbsp;|&nbsp;	<a href="../faq.lxp">FAQ</a>	&nbsp;|&nbsp;        <a href="../writeforus.lxp">Write for Us.</a>        &nbsp;|&nbsp;        <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>Base classes</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="Qt Class Hierarchy"HREF="c2591.htm"><LINKREL="PREVIOUS"TITLE="Qt Class Hierarchy"HREF="c2591.htm"><LINKREL="NEXT"TITLE="Application classes"HREF="x2693.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=c2591_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 10. Qt Class Hierarchy</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x2693_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Base classes</A></H1><P>You won't often create objects from Qt base classes,      but you might need to subclass QObject now and then. The basic      classes that Qt is built on are QObject and QEvent. There are several      other classes used for the construction of high-level datatypes,      and a large number of other classes that support working with,      for instance, fonts, images and colors.</P><P>QObject brings together support for: </P><P></P><UL><LI><P>Signals and slots</P></LI><LI><P>Timers</P></LI><LI><P>Object ownership hierarchy</P></LI><LI><P>Event handling and event filters</P></LI><LI><P>Introspection</P></LI><LI><P>Properties</P></LI></UL><P>Signals and slots are meant for communication between      objects&#8212;for instance, when a button is pressed, certain other      objects must be notified. Events, on the other hand, notify      objects of general actions of the user, such as key presses      or mouse movements; events do not necessarily originate with      objects.</P><P>The linchpin of the event handling mechanism is the class      representing the information associated with an event, such as      the position of the mouse. This is the QEvent class; there are a      whole slew of specialized subclasses like QPaintEvent,      QFocusEvent, QMouseEvent, QWheelEvent and QKeyEvent that do the      rounds of all interested objects. For instance, a keypress is      first passed to the application object, based on QApplication.      From there it trickles down the whole widget ownership hierarchy      until one widget &#8216;consumes' it &#8212;that is, reacts to the event and doesn't      send it on by calling the <TTCLASS="FUNCTION">event(QEvent)</TT>      method.</P><P>See the next listing for an example of reacting to mouse      presses and movements. Note also that if the window is      obscured and remapped, the paintEvent method is fired&#8212;      this will obliterate your whole beautiful drawing.</P><DIVCLASS="EXAMPLE"></A><P><B>Example 10-1. event1.py - handling mouse events in PyQt</B></P><PRECLASS="PROGRAMLISTING">## event1.py#from qt import *import sysclass Painting(QWidget):    def __init__(self, *args):        apply(QWidget.__init__,(self, ) + args)    def paintEvent(self, ev):        self.p = QPainter()        self.p.begin(self)        self.p.fillRect(self.rect(), QBrush(Qt.white))        self.p.flush()        self.p.end()    def mouseMoveEvent(self, ev):        self.p = QPainter()        self.p.begin(self)        self.p.drawLine(self.currentPos, ev.pos())        self.currentPos=QPoint(ev.pos())        self.p.flush()        self.p.end()    def mousePressEvent(self, ev):        self.p = QPainter()        self.p.begin(self)        self.p.drawPoint(ev.pos())        self.currentPos=QPoint(ev.pos())        self.p.flush()        self.p.end()class MainWindow(QMainWindow):    def __init__(self, *args):        apply(QMainWindow.__init__, (self,) + args)        self.painting=Painting(self)        self.setCentralWidget(self.painting)def main(args):    app=QApplication(args)    win=MainWindow()    win.show()    app.connect(app, SIGNAL("lastWindowClosed()"),                app, SLOT("quit()"))    app.exec_loop()if __name__=="__main__":    main(sys.argv)      </PRE></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>event1.py</P></DIV></P></DIV><P>In handling methods such as <TTCLASS="FUNCTION">mousePressEvent()</TT>,      It is customary to use <TTCLASS="PARAMETER"><I>ev</I></TT> as the name      for the <TTCLASS="CLASSNAME">QEvent</TT> parameter. In      this example, all mouse press events and mouse move events are      consumed by the <TTCLASS="CLASSNAME">Painting</TT> class.</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=c2591_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=x2693_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Qt Class Hierarchy</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">Application classes</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

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