index.lxp@lxpwrap=x8073_252ehtm.htm

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

HTM
283
字号
    <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>Initiating drags</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="Drag and drop"HREF="c8016.htm"><LINKREL="PREVIOUS"TITLE="Drag and drop"HREF="c8016.htm"><LINKREL="NEXT"TITLE="Conclusion"HREF="x8095.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=c8016_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 23. Drag and drop</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x8095_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Initiating drags</A></H1><P>Initiating drag operations is a bit more      difficult, because you must determine when exactly a drag      begins. Users expect that holding down the mouse-button on the      object they want to drag and moving it should start dragging.      However, everyone moves the mouse very slightly, even if just      single-clicking on an object. Thus, we must determine how many      pixels the mouse pointer should move (while the first mouse      button is depressed) to enter a drag operation.</P><P>We cannot do this using      <TTCLASS="CLASSNAME">QMultiLineEdit</TT>. A class like      <TTCLASS="CLASSNAME">QIconView</TT> offers special support for      starting drags by allowing you to re-implement the      <TTCLASS="FUNCTION">dragObject</TT> function:</P><PRECLASS="PROGRAMLISTING">class MyIconView(QIconView):      def dragObject(self):          return QTextDrag(self.currentItem().text(), self)    </PRE><P>Once you have determined what should start      a drag (and this can be anything, really), starting a drag      operation is a simple matter of creating a new      <TTCLASS="CLASSNAME">QDragObject</TT>. This could be, for instance,      a <TTCLASS="CLASSNAME">QTextDrag</TT> or a      <TTCLASS="CLASSNAME">QImageDrag</TT>. Take care that you keep a      reference to the drag object&#8212;if you delete it, or allow it      to go out of scope, it will disappear.</P><P>In the next example, simply moving a mouse      will start a drag operation:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 23-2. Drag and drop</B></P><PRECLASS="PROGRAMLISTING">#!/usr/bin/env pythonfrom sys import argvfrom qt import *class DropDemo(QListView):    def __init__(self,parent=None,name=None):        QListView.__init__(self,parent,name)        self.setAcceptDrops(1)        self.setGeometry(10,10,100,60)        self.addColumn("Column 1")        self.i = QListViewItem(self, "Hello")        self.ii = QListViewItem(self, "Hello 2")        r = self.itemRect(self.i)        r = self.itemRect(self.ii)            def dragEnterEvent(self, event):        event.accept(QTextDrag.canDecode(event))      def dropEvent(self, event):        text = QString()        pos = QPoint(event.pos().x(), event.pos().y() - self.header().height())        item = self.itemAt(pos)        text=QString()     def mouseMoveEvent(self, event):        self.startDrag()    def startDrag(self):        d = QTextDrag("blabla",self) # keep a reference to d        d.dragCopy() if __name__ == '__main__':    a = QApplication(argv)    w = DropDemo()    w.setCaption("Drag and Drop Test")    w.resize(120,80)    a.setMainWidget(w)    w.show()    a.exec_loop()        </PRE></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=c8016_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=x8095_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Drag and drop</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c8016_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 + =
减小字号Ctrl + -
显示快捷键?