dnd.html
来自「qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人」· HTML 代码 · 共 347 行 · 第 1/2 页
HTML
347 行
<p> As suggested in the DXF example above, drag-and-drop is not limited to text and images. Any information can be dragged and dropped. To drag information between applications, the two applications need a way to agree on the type of information they both understand. The way they do it is using <i><a href="http://www.rfc-editor.org/rfc/rfc1341.txt">MIME types</a></i> - the drag source provides a list of MIME types that it can produce (ordered from most appropriate to least appropriate), and the drop target chooses which of those it can handle. For example, QTextDrag provides support for the "<tt>text/plain</tt>" MIME type (ordinary unformatted text), and the Unicode formats "<tt>text/utf16</tt>" and "<tt>text/utf8</tt>"; QImageDrag provides for "<tt>image/</tt><tt>*</tt>", where <tt>*</tt> is any image format that <a href="qimageio.html">QImageIO</a> supports; and the QUriDrag subclass provides "<tt>text/uri-list</tt>", a standard format for transferring a list of filenames (or URLs).<p> To implement drag-and-drop of some type of information for which there is no available QDragObject subclass, the first and most important step is to look for existing formats that are appropriate - the Internet Assigned Numbers Authority (<a href="http://www.iana.org">IANA</a>) provides a <a href="http://www.isi.edu/in-notes/iana/assignments/media-types/"> hierarchical list of MIME media types</a> at the Information Sciences Institute (<a href="http://www.isi.edu">ISI</a>). This maximizes inter-operability of your software with other software now and in the future.<p> To support an additional media type, subclass either QDragObject or QStoredDrag. Subclass QDragObject when you need to provide support for multiple media types. Subclass the simpler QStoredDrag when one type is sufficient.<p> Subclasses of QDragObject will override the <a href="qmimesource.html#17ce96">const char* format(int i) const</a> and <a href="qmimesource.html#81afea">QByteArray encodedData(const char* mimetype) const</a> members, and provide a set-method to encode the media data and static members canDecode() and decode() to decode incoming data, similar to <a href="qimagedrag.html#c35128">bool canDecode(QMimeSource*) const</a> and <a href="qimagedrag.html#037fd2">QByteArray decode(QMimeSource*) const</a> of QImageDrag. Of course, you can provide drag-only or drop-only support for a media type by omitting some of these methods.<p> Subclasses of QStoredDrag provide a set-method to encode the media data and the same static members canDecode() and decode() to decode incoming data.<p> <a name=advanced><h3>Advanced Drag-and-Drop</h3></a><p> In the clipboard model, the user can <em>cut</em> or <em>copy</em> the source information, then later paste it. Similarly in the drag-and-drop model, the user can drag a <em>copy</em> of the information or they can drag the information itself to a new place (<em>moving</em> it). The drag-and-drop model however has an additional complication for the programmer: the program doesn't know whether the user want to cut or copy until the drop (paste) is done! For dragging between applications, it makes no difference, but for dragging within an application, the application must take a little extra care not to tread on its own feet. For example, to drag text around in a document, the drag start point and the drop event might look like this:<p> <pre> void MyEditor::startDragging() { <a href="qdragobject.html">QDragObject</a> *d = new <a href="qtextdrag.html">QTextDrag</a>(myHighlightedText(), this); if ( d-><a href="qdragobject.html#61c2fe">drag</a>() && d-><a href="qdragobject.html#cdb79f">target</a>() != this ) cutMyHighlightedText(); } void MyEditor::dropEvent(<a href="qdropevent.html">QDropEvent</a>* event) { <a href="qstring.html">QString</a> text; if ( <a href="qtextdrag.html#b02128">QTextDrag::decode</a>(event, text) ) { if ( event-><a href="qdropevent.html#00626c">source</a>() == this && event-><a href="qdropevent.html#d9f8b7">action</a>() == QDropEvent::Move ) { // Careful not to tread on my own feet event-><a href="qdropevent.html#d37a0e">acceptAction</a>(); moveMyHighlightedTextTo(event-><a href="qdropevent.html#b3ba77">pos</a>()); } else { pasteTextAt(text, event-><a href="qdropevent.html#b3ba77">pos</a>()); } } }</pre><p> Some widgets are more specific than just a "yes" or "no" response when data is dragged onto them. For example, a CAD program might only accept drops of text onto text objects in the view. In these cases, the <a href="qwidget.html#3b47df">dragMoveEvent()</a> is used and an <em>area</em> is given for which the drag is accepted or ignored: <pre> void MyWidget::dragMoveEvent(<a href="qdragmoveevent.html">QDragMoveEvent</a>* event) { if ( <a href="qtextdrag.html#48ca8d">QTextDrag::canDecode</a>(event) ) { MyCadItem* item = findMyItemAt(event-><a href="qdropevent.html#b3ba77">pos</a>()); if ( item ) event-><a href="qdragmoveevent.html#250cc4">accept</a>(); } }</pre><p> If the computations to find objects are particularly slow, you might find improved performance if you tell the system an area for which you promise the acceptance persists: <pre> void MyWidget::dragMoveEvent(<a href="qdragmoveevent.html">QDragMoveEvent</a>* event) { if ( <a href="qtextdrag.html#48ca8d">QTextDrag::canDecode</a>(event) ) { MyCadItem* item = findMyItemAt(event-><a href="qdropevent.html#b3ba77">pos</a>()); if ( item ) { <a href="qrect.html">QRect</a> r = item->areaRelativeToMeClippedByAnythingInTheWay(); if ( item->type() == MyTextType ) event-><a href="qdragmoveevent.html#250cc4">accept</a>( r ); else event-><a href="qdragmoveevent.html#2a033b">ignore</a>( r ); } } }</pre><p> The dragMoveEvent() can also be used if you need to give visual feedback as the drag progresses, to start timers to scroll the window, or whatever you need (don't forget to stop scrolling timers in a dragLeaveEvent() though).<p> For a complete example of drag and drop, examine the <code>dragdrop</code> example program, or the QMultiLineEdit widget source code.<p> <h3>Inter-operating with other applications</h3> On X11, the public <a class="r" href="http://www.newplanetsoftware.com/xdnd/">XDND protocol</a> is used, while on Windows Qt uses the OLE standard. On X11, XDND uses MIME, so no translation is necessary. The Qt API is the same regardless of the platform. On Windows, MIME-aware applications can communicate by using clipboard format names that are MIME types. Already some Windows applications use MIME naming conventions for their clipboard formats. Internally, Qt has facilities for translating proprietary clipboard formats to and from MIME types. This interface will be made public at some time, but if you need to do such translations now, contact your Qt Technical Support service.<p> On X11, Qt also supports drops via the Motif Drag&Drop Protocol. The implementation incorporates some code that was originally written by Daniel Dardailler, and adapted for Qt by Matt Koss <koss@napri.sk> and Trolltech. Here is the original copyright notice:<p> Copyright 1996 Daniel Dardailler.<p> Permission to use, copy, modify, distribute, and sell this software for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of Daniel Dardailler not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. Daniel Dardailler makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.<p> Modifications Copyright 1999 Matt Koss, under the same license as above.<p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?