dnd.html

来自「qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人」· HTML 代码 · 共 347 行 · 第 1/2 页

HTML
347
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit -  Drag and Drop</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><p><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b>  <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align="center"> Drag and Drop</h1><br clear="all">  Drag-and-drop is a direct-manipulation model for allowing the user to  transfer information between and within applications. It is similar  in function to the cut-and-paste clipboard model.<p>  <h3>Dragging</h3><p>  To start a drag, for example in a <a href="qwidget.html#a51d92">mouse motion event</a>, create an object of the  QDragObject subclass appropriate for your media, such as  QTextDrag for text and QImageDrag for images. Then call  the drag() method. This is all you need for simple dragging  of existing types.<p>  For example, to start dragging some text from a widget:  <pre>  void MyWidget::startDragging()  {    <a href="qdragobject.html">QDragObject</a> *d = new <a href="qtextdrag.html">QTextDrag</a>( myHighlightedText(), this );    d-&gt;<a href="qdragobject.html#9d27cf">dragCopy</a>();    // do NOT delete d.  }</pre><p>  Note that the QDragObject is not deleted after the drag.  The  QDragObject needs to persist after the drag is apparently finished -  it may still be communicating with another process.  Eventually Qt  will delete the object.  If the widget owning the drag object is  deleted before then, any pending drop will be cancelled and the drag  object deleted.  For this reason, you should be careful what the  object references.<p>  <h3>Dropping</h3><p>  To be able to receive media dropped on a widget, call  <a href="qwidget.html#8169cb">setAcceptDrops(TRUE)</a>  for the widget (eg. in its constructor), and override the  event handler methods  <a href="qwidget.html#c8639f">dragEnterEvent()</a>,  <a href="qwidget.html#3b47df">dragMoveEvent()</a>,  <a href="qwidget.html#fdd48c">dragLeaveEvent()</a>, and  <a href="qwidget.html#e9b4c3">dropEvent()</a>.<p>  For example, to accept text and image drops:  <pre>  MyWidget::MyWidget(...) :    <a href="qwidget.html">QWidget</a>(...)  {    ...    setAcceptDrops(TRUE);  }  void MyWidget::dragEnterEvent(<a href="qdragenterevent.html">QDragEnterEvent</a>* event)  {    event-&gt;<a href="qdropevent.html#da0c5a">accept</a>(        <a href="qtextdrag.html#48ca8d">QTextDrag::canDecode</a>(event) ||        <a href="qimagedrag.html#c35128">QImageDrag::canDecode</a>(event)    );  }  void MyWidget::dropEvent(<a href="qdropevent.html">QDropEvent</a>* event)  {    <a href="qimage.html">QImage</a> image;    <a href="qstring.html">QString</a> text;    if ( <a href="qimagedrag.html#037fd2">QImageDrag::decode</a>(event, image) ) {      insertImageAt(image, event-&gt;<a href="qdropevent.html#b3ba77">pos</a>());    } else if ( <a href="qtextdrag.html#b02128">QTextDrag::decode</a>(event, text) ) {      insertTextAt(text, event-&gt;<a href="qdropevent.html#b3ba77">pos</a>());    }  }</pre><p>  <h3>The Clipboard</h3><p>  The QDragObject, QDragEnterEvent, QDragMoveEvent, and QDropEvent  classes are all subclasses of QMimeSource - the class of objects  which provide  typed  information.  If you base your data  transfers on QDragObject, you not only get drag-and-drop, but you  also get traditional cut-and-paste for free - the QClipboard has  two functions:  <pre> setData(<a href="qmimesource.html">QMimeSource</a>*)</pre> and  <pre> <a href="qmimesource.html">QMimeSource</a>* data()const</pre>.  With these, you can trivially  put your drag-and-drop oriented information on the clipboard:  <pre>  void MyWidget::copy()  {    <a href="qapplication.html#1c95b5">QApplication::clipboard</a>()-&gt;setData(        new QTextDrag(myHighlightedText())    );  }  void MyWidget::paste()  {    <a href="qstring.html">QString</a> text;    if ( <a href="qtextdrag.html#b02128">QTextDrag::decode</a>(<a href="qapplication.html#1c95b5">QApplication::clipboard</a>()-&gt;data(), text) )        insertText( text );  }</pre><p>  You can even use QDragObject subclasses as part of file IO.  For example,  if your application has a subclass of QDragObject that encodes  CAD designs in DXF format, your saving and loading code might be:  <pre>  void MyWidget::save()  {    <a href="qfile.html">QFile</a> out(current_file_name);    out.<a href="qfile.html#255995">open</a>(IO_WriteOnly);    MyCadDrag tmp(current_design);    out.<a href="qfile.html#a44b6a">writeBlock</a>( tmp-&gt;encodedData( "image/x-dxf" );  }  void MyWidget::load()  {    <a href="qfile.html">QFile</a> in(current_file_name);    in.<a href="qfile.html#255995">open</a>(IO_ReayOnly);    if ( !MyCadDrag::decode(in.<a href="qiodevice.html#c272a4">readAll</a>(), current_design) ) {        <a href="qmessagebox.html#63edba">QMessageBox::warning</a>( this, "Format error",            tr("The file \"%1\" is not in any supported format")             .arg(current_file_name)        );    }  }</pre><p>  Note how the QDragObject subclass is called "MyCadDrag", not  "MyDxfDrag" - because in the future you might extend it to  provide DXF, DWG, SVF, WMF, or even QPicture data to other  applications.<p>  <h3>Drag-and-drop action</h3><p>  In the simpler cases, the target of a drag-and-drop receives  a copy of the data being dragged and the source decides whether  to delete the original.  This is the "Copy" action in QDropEvent.  The target may also choose to understand other actions, specifically  the Move and Link actions.  If the target understands the Move action,  <em>it</em>  is responsible for both the copy and delete operations and the source  will not attempt to delete the data itself.  If the target  understands the Link, it stores some kind of reference to the original  information, and again the source does not delete the original.  The most common use of drag-and-drop actions is when performing a Move  within the same widget - see the  <a href=#advanced>Advanced Drag-and-Drop</a> section below.<p>  The other major use of drag actions is when using a reference  type such as text/uri-list, where the dragged data is actually references  to files or objects.<p>  <h3>Adding new drag-and-drop types</h3>

⌨️ 快捷键说明

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