1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- porting4-dnd.qdoc -->
<title>Qt 4.8: Porting to Qt 4 - Drag and Drop</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li>Porting to Qt 4 - Drag and Drop</li>
</ul>
</div>
</div>
<div class="content mainContent">
<link rel="prev" href="porting4-virtual-functions.html" />
<link rel="next" href="porting4-designer.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="porting4-virtual-functions.html">Porting to Qt 4 - Virtual Functions</a>
<a class="nextPage" href="porting4-designer.html">Porting UI Files to Qt 4</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#dragging">Dragging</a></li>
<li class="level1"><a href="#dropping">Dropping</a></li>
<li class="level1"><a href="#mime-types-and-data">MIME Types and Data</a></li>
</ul>
</div>
<h1 class="title">Porting to Qt 4 - Drag and Drop</h1>
<span class="subtitle"></span>
<!-- $$$porting4-dnd.html-description -->
<div class="descr"> <a name="details"></a>
<p>Qt 4 introduces a new set of classes to handle drag and drop operations that aim to be easier to use than their counterparts in Qt 3. As a result, the way that drag and drop is performed is quite different to the way developers of Qt 3 applications have come to expect. In this guide, we show the differences between the old and new APIs and indicate where applications need to be changed when they are ported to Qt 4.</p>
<a name="dragging"></a>
<h2>Dragging</h2>
<p>In Qt 3, drag operations are encapsulated by <tt>QDragObject</tt> (see <a href="q3dragobject.html" class="compat">Q3DragObject</a>) and its subclasses. These objects are typically constructed on the heap in response to mouse click or mouse move events, and ownership of them is transferred to Qt so that they can be deleted when the corresponding drag and drop operations have been completed. The drag source has no control over how the drag and drop operation is performed once the object's <a href="q3dragobject.html#drag">drag()</a> function is called, and it receives no information about how the operation ended.</p>
<pre class="cpp"> <span class="type">void</span> MyQt3Widget<span class="operator">::</span>customStartDragFunction()
{
<span class="type">QDragObject</span> <span class="operator">*</span>d <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QTextDrag</span>( myHighlightedText()<span class="operator">,</span> <span class="keyword">this</span> );
d<span class="operator">-</span><span class="operator">></span>dragCopy();
<span class="comment">// do NOT delete d.</span>
}</pre>
<p>Similarly, in Qt 4, drag operations are also initiated when a <a href="qdrag.html">QDrag</a> object is constructed and its <a href="qdrag.html#exec">exec()</a> function is called. In contrast, these objects are typically constructed on the stack rather than the heap since each drag and drop operation is performed synchronously as far as the drag source is concerned. One key benefit of this is that the drag source can receive information about how the operation ended from the value returned by <a href="qdrag.html#exec">exec()</a>.</p>
<pre class="cpp"> <span class="type">void</span> MyWidget<span class="operator">::</span>mousePressEvent(<span class="type"><a href="qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event)
{
<span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">></span>button() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>LeftButton) {
<span class="type"><a href="qdrag.html">QDrag</a></span> <span class="operator">*</span>drag <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdrag.html">QDrag</a></span>(<span class="keyword">this</span>);
<span class="type"><a href="qmimedata.html">QMimeData</a></span> <span class="operator">*</span>mimeData <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qmimedata.html">QMimeData</a></span>;
mimeData<span class="operator">-</span><span class="operator">></span>setText(text);
mimeData<span class="operator">-</span><span class="operator">></span>setImageData(image);
drag<span class="operator">-</span><span class="operator">></span>setMimeData(mimeData);
drag<span class="operator">-</span><span class="operator">></span>setPixmap(iconPixmap);
<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>DropAction dropAction <span class="operator">=</span> drag<span class="operator">-</span><span class="operator">></span>exec();
...
event<span class="operator">-</span><span class="operator">></span>accept();
}
}</pre>
<p>A key difference in the above code is the use of the <a href="qmimedata.html">QMimeData</a> class to hold information about the data that is transferred. Qt 3 relies on subclasses of <tt>QDragObject</tt> to provide support for specific MIME types; in Qt 4, the use of <a href="qmimedata.html">QMimeData</a> as a generic container for data makes the relationship between MIME type and data more tranparent. <a href="qmimedata.html">QMimeData</a> is described in more detail later in this document.</p>
<a name="dropping"></a>
<h2>Dropping</h2>
<p>In both Qt 3 and Qt 4, it is possible to prepare a custom widget to accept dropped data by enabling the <a href="qwidget.html#acceptDrops-prop">acceptDrops</a> property of a widget, usually in the widget's constructor. As a result, the widget will receive drag enter events that can be handled by its <a href="qwidget.html#dragEnterEvent">dragEnterEvent()</a> function. As in Qt 3, custom widgets in Qt 4 handle these events by determining whether the data supplied by the drag and drop operation can be dropped onto the widget. Since the classes used to encapsulate MIME data are different in Qt 3 and Qt 4, the exact implementations differ.</p>
<p>In Qt 3, the drag enter event is handled by checking whether each of the standard <tt>QDragObject</tt> subclasses can decode the data supplied, and indicating success or failure of these checks via the event's <a href="qdragmoveevent.html#accept">accept()</a> function, as shown in this simple example:</p>
<pre class="cpp"> <span class="type">void</span> MyQt3Widget<span class="operator">::</span>dragEnterEvent(<span class="type"><a href="qdragenterevent.html">QDragEnterEvent</a></span><span class="operator">*</span> event)
{
event<span class="operator">-</span><span class="operator">></span>accept(
<span class="type">QTextDrag</span><span class="operator">::</span>canDecode(event) <span class="operator">|</span><span class="operator">|</span>
<span class="type">QImageDrag</span><span class="operator">::</span>canDecode(event)
);
}</pre>
<p>In Qt 4, you can examine the MIME type describing the data to determine whether the widget should accept the event or, for common data types, you can use convenience functions:</p>
<pre class="cpp"> <span class="type">void</span> MyWidget<span class="operator">::</span>dragEnterEvent(<span class="type"><a href="qdragenterevent.html">QDragEnterEvent</a></span> <span class="operator">*</span>event)
{
<span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">></span>mimeData()<span class="operator">-</span><span class="operator">></span>hasText() <span class="operator">|</span><span class="operator">|</span> event<span class="operator">-</span><span class="operator">></span>mimeData()<span class="operator">-</span><span class="operator">></span>hasImage())
event<span class="operator">-</span><span class="operator">></span>acceptProposedAction();
}</pre>
<p>The widget has some control over the type of drag and drop operation to be performed. In the above code, the action proposed by the drag source is accepted, but <a href="dnd.html#overriding-proposed-actions">this can be overridden</a> if required.</p>
<p>In both Qt 3 and Qt 4, it is necessary to accept a given drag event in order to receive the corresponding drop event. A custom widget in Qt 3 that can accept dropped data in the form of text or images might provide an implementation of <a href="qwidget.html#dropEvent">dropEvent()</a> that looks like the following:</p>
<pre class="cpp"> <span class="type">void</span> MyQt3Widget<span class="operator">::</span>dropEvent(<span class="type"><a href="qdropevent.html">QDropEvent</a></span><span class="operator">*</span> event)
{
<span class="type"><a href="qimage.html">QImage</a></span> image;
<span class="type"><a href="qstring.html">QString</a></span> text;
<span class="keyword">if</span> ( <span class="type">QImageDrag</span><span class="operator">::</span>decode(event<span class="operator">,</span> image) ) {
insertImageAt(image<span class="operator">,</span> event<span class="operator">-</span><span class="operator">></span>pos());
} <span class="keyword">else</span> <span class="keyword">if</span> ( <span class="type">QTextDrag</span><span class="operator">::</span>decode(event<span class="operator">,</span> text) ) {
insertTextAt(text<span class="operator">,</span> event<span class="operator">-</span><span class="operator">></span>pos());
}
}</pre>
<p>In Qt 4, the event is handled in a similar way:</p>
<pre class="cpp"> <span class="type">void</span> MyWidget<span class="operator">::</span>dropEvent(<span class="type"><a href="qdropevent.html">QDropEvent</a></span> <span class="operator">*</span>event)
{
<span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">></span>mimeData()<span class="operator">-</span><span class="operator">></span>hasText())
dataLabel<span class="operator">-</span><span class="operator">></span>setText(event<span class="operator">-</span><span class="operator">></span>mimeData()<span class="operator">-</span><span class="operator">></span>text());
<span class="keyword">else</span> <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">></span>mimeData()<span class="operator">-</span><span class="operator">></span>hasImage()) {
<span class="type"><a href="qvariant.html">QVariant</a></span> imageData <span class="operator">=</span> event<span class="operator">-</span><span class="operator">></span>mimeData()<span class="operator">-</span><span class="operator">></span>imageData();
dataLabel<span class="operator">-</span><span class="operator">></span>setPixmap(qvariant_cast<span class="operator"><</span><span class="type"><a href="qpixmap.html">QPixmap</a></span><span class="operator">></span>(imageData));
}
event<span class="operator">-</span><span class="operator">></span>acceptProposedAction();
}</pre>
<p>It is also possible to extract data stored for a particular MIME type if it was specified by the drag source.</p>
<a name="mime-types-and-data"></a>
<h2>MIME Types and Data</h2>
<p>In Qt 3, data to be transferred in drag and drop operations is encapsulated in instances of <tt>QDragObject</tt> and its subclasses, representing specific data formats related to common MIME type and subtypes.</p>
<p>In Qt 4, only the <a href="qmimedata.html">QMimeData</a> class is used to represent data, providing a container for data stored in multiple formats, each associated with a relevant MIME type. Since arbitrary MIME types can be specified, there is no need for an extensive class hierarchy to represent different kinds of information. Additionally, <a href="qmimedata.html">QMimeData</a> it provides some convenience functions to allow the most common data formats to be stored and retrieved with less effort than for arbitrary MIME types.</p>
</div>
<!-- @@@porting4-dnd.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="porting4-virtual-functions.html">Porting to Qt 4 - Virtual Functions</a>
<a class="nextPage" href="porting4-designer.html">Porting UI Files to Qt 4</a>
</p>
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
The documentation provided herein is licensed under the terms of the
<a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
License version 1.3</a> as published by the Free Software Foundation.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|