source: trunk/doc/html/simple_dd-example.html@ 190

Last change on this file since 190 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 11.0 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/iconview/simple_dd/simple_dd.doc:1 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Drag and Drop (Simple)</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Drag and Drop (Simple)</h1>
33
34
35<p>
36This provides a very simple example of Qt's drag and drop
37functionality.
38<p> For a more complete example see the <a href="dragdrop-example.html">Drag and Drop example</a>.
39<p> <hr>
40<p> Header file:
41<p> <pre>/****************************************************************************
42** $Id: simple_dd-example.html 2051 2007-02-21 10:04:20Z chehrlic $
43**
44** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
45**
46** This file is part of an example program for Qt. This example
47** program may be used, distributed and modified without limitation.
48**
49*****************************************************************************/
50
51#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
52#include &lt;<a href="qcursor-h.html">qcursor.h</a>&gt;
53#include &lt;<a href="qsplitter-h.html">qsplitter.h</a>&gt;
54#include &lt;<a href="qlistbox-h.html">qlistbox.h</a>&gt;
55#include &lt;<a href="qiconview-h.html">qiconview.h</a>&gt;
56#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
57
58class QDragEnterEvent;
59class QDragDropEvent;
60
61
62class DDListBox : public <a href="qlistbox.html">QListBox</a>
63{
64 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
65public:
66 DDListBox( <a href="qwidget.html">QWidget</a> * parent = 0, const char * name = 0, WFlags f = 0 );
67 // Low-level drag and drop
68 void dragEnterEvent( <a href="qdragenterevent.html">QDragEnterEvent</a> *evt );
69 void dropEvent( <a href="qdropevent.html">QDropEvent</a> *evt );
70 void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *evt );
71 void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
72private:
73 int dragging;
74};
75
76
77class DDIconViewItem : public <a href="qiconviewitem.html">QIconViewItem</a>
78{
79public:
80 DDIconViewItem( <a href="qiconview.html">QIconView</a> *parent, const <a href="qstring.html">QString</a>&amp; text, const <a href="qpixmap.html">QPixmap</a>&amp; icon ) :
81 <a href="qiconviewitem.html">QIconViewItem</a>( parent, text, icon ) {}
82 DDIconViewItem( <a href="qiconview.html">QIconView</a> *parent, const <a href="qstring.html">QString</a> &amp;text ) :
83 <a href="qiconviewitem.html">QIconViewItem</a>( parent, text ) {}
84 // High-level drag and drop
85 bool acceptDrop( const <a href="qmimesource.html">QMimeSource</a> *mime ) const;
86 void dropped( <a href="qdropevent.html">QDropEvent</a> *evt, const <a href="qvaluelist.html">QValueList</a>&lt;QIconDragItem&gt;&amp; );
87};
88
89
90class DDIconView : public <a href="qiconview.html">QIconView</a>
91{
92 Q_OBJECT
93public:
94 DDIconView( <a href="qwidget.html">QWidget</a> * parent = 0, const char * name = 0, WFlags f = 0 ) :
95 <a href="qiconview.html">QIconView</a>( parent, name, f ) {}
96 // High-level drag and drop
97 <a href="qdragobject.html">QDragObject</a> *dragObject();
98public slots:
99 void slotNewItem( <a href="qdropevent.html">QDropEvent</a> *evt, const <a href="qvaluelist.html">QValueList</a>&lt;QIconDragItem&gt;&amp; list );
100};
101
102</pre>
103
104<p> <hr>
105<p> Implementation:
106<p> <pre>/****************************************************************************
107** $Id: simple_dd-example.html 2051 2007-02-21 10:04:20Z chehrlic $
108**
109** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
110**
111** This file is part of an example program for Qt. This example
112** program may be used, distributed and modified without limitation.
113**
114*****************************************************************************/
115
116#include "main.h"
117
118const char* red_icon[]={
119"16 16 2 1",
120"r c red",
121". c None",
122"................",
123"................",
124"..rrrrrrrrrrrr..",
125"..rrrrrrrrrrrr..",
126"..rrrrrrrrrrrr..",
127"..rrr......rrr..",
128"..rrr......rrr..",
129"..rrr......rrr..",
130"..rrr......rrr..",
131"..rrr......rrr..",
132"..rrr......rrr..",
133"..rrrrrrrrrrrr..",
134"..rrrrrrrrrrrr..",
135"..rrrrrrrrrrrr..",
136"................",
137"................"};
138
139const char* blue_icon[]={
140"16 16 2 1",
141"b c blue",
142". c None",
143"................",
144"................",
145"..bbbbbbbbbbbb..",
146"..bbbbbbbbbbbb..",
147"..bbbbbbbbbbbb..",
148"..bbb......bbb..",
149"..bbb......bbb..",
150"..bbb......bbb..",
151"..bbb......bbb..",
152"..bbb......bbb..",
153"..bbb......bbb..",
154"..bbbbbbbbbbbb..",
155"..bbbbbbbbbbbb..",
156"..bbbbbbbbbbbb..",
157"................",
158"................"};
159
160const char* green_icon[]={
161"16 16 2 1",
162"g c green",
163". c None",
164"................",
165"................",
166"..gggggggggggg..",
167"..gggggggggggg..",
168"..gggggggggggg..",
169"..ggg......ggg..",
170"..ggg......ggg..",
171"..ggg......ggg..",
172"..ggg......ggg..",
173"..ggg......ggg..",
174"..ggg......ggg..",
175"..gggggggggggg..",
176"..gggggggggggg..",
177"..gggggggggggg..",
178"................",
179"................"};
180
181
182// ListBox -- low level drag and drop
183
184<a name="f589"></a>DDListBox::DDListBox( <a href="qwidget.html">QWidget</a> * parent, const char * name, WFlags f ) :
185 <a href="qlistbox.html">QListBox</a>( parent, name, f )
186{
187 <a href="qwidget.html#setAcceptDrops">setAcceptDrops</a>( TRUE );
188 dragging = FALSE;
189}
190
191
192<a name="x2829"></a>void DDListBox::<a href="qwidget.html#dragEnterEvent">dragEnterEvent</a>( <a href="qdragenterevent.html">QDragEnterEvent</a> *evt )
193{
194<a name="x2827"></a> if ( QTextDrag::<a href="qtextdrag.html#canDecode">canDecode</a>( evt ) )
195<a name="x2819"></a> evt-&gt;<a href="qdragmoveevent.html#accept">accept</a>();
196}
197
198
199<a name="x2830"></a>void DDListBox::<a href="qwidget.html#dropEvent">dropEvent</a>( <a href="qdropevent.html">QDropEvent</a> *evt )
200{
201 <a href="qstring.html">QString</a> text;
202
203<a name="x2828"></a> if ( QTextDrag::<a href="qtextdrag.html#decode">decode</a>( evt, text ) )
204 <a href="qlistbox.html#insertItem">insertItem</a>( text );
205}
206
207
208<a name="x2832"></a>void DDListBox::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *evt )
209{
210 QListBox::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( evt );
211 dragging = TRUE;
212}
213
214
215<a name="x2831"></a>void DDListBox::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * )
216{
217 if ( dragging ) {
218 <a href="qdragobject.html">QDragObject</a> *d = new <a href="qtextdrag.html">QTextDrag</a>( currentText(), this );
219<a name="x2818"></a> d-&gt;<a href="qdragobject.html#dragCopy">dragCopy</a>(); // do NOT delete d.
220 dragging = FALSE;
221 }
222}
223
224
225// IconViewIcon -- high level drag and drop
226
227
228<a name="x2822"></a>bool DDIconViewItem::<a href="qiconviewitem.html#acceptDrop">acceptDrop</a>( const <a href="qmimesource.html">QMimeSource</a> *mime ) const
229{
230<a name="x2825"></a> if ( mime-&gt;<a href="qmimesource.html#provides">provides</a>( "text/plain" ) )
231 return TRUE;
232 return FALSE;
233}
234
235
236<a name="x2823"></a>void DDIconViewItem::<a href="qiconviewitem.html#dropped">dropped</a>( <a href="qdropevent.html">QDropEvent</a> *evt, const <a href="qvaluelist.html">QValueList</a>&lt;QIconDragItem&gt;&amp; )
237{
238 <a href="qstring.html">QString</a> label;
239
240 if ( QTextDrag::<a href="qtextdrag.html#decode">decode</a>( evt, label ) )
241 setText( label );
242}
243
244
245// IconView -- high level drag and drop
246
247<a name="x2820"></a>QDragObject *DDIconView::<a href="qiconview.html#dragObject">dragObject</a>()
248{
249 return new <a href="qtextdrag.html">QTextDrag</a>( <a href="qiconview.html#currentItem">currentItem</a>()-&gt;text(), this );
250}
251
252void <a name="f588"></a>DDIconView::slotNewItem( <a href="qdropevent.html">QDropEvent</a> *evt, const <a href="qvaluelist.html">QValueList</a>&lt;QIconDragItem&gt;&amp; )
253{
254 <a href="qstring.html">QString</a> label;
255
256 if ( QTextDrag::<a href="qtextdrag.html#decode">decode</a>( evt, label ) ) {
257 DDIconViewItem *item = new DDIconViewItem( this, label );
258<a name="x2824"></a> item-&gt;<a href="qiconviewitem.html#setRenameEnabled">setRenameEnabled</a>( TRUE );
259 }
260}
261
262
263
264int main( int argc, char *argv[] )
265{
266 <a href="qapplication.html">QApplication</a> app( argc, argv );
267
268 // Create and show the widgets
269 <a href="qsplitter.html">QSplitter</a> *split = new <a href="qsplitter.html">QSplitter</a>();
270 DDIconView *iv = new DDIconView( split );
271 (void) new DDListBox( split );
272 app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( split );
273 split-&gt;<a href="qwidget.html#resize">resize</a>( 600, 400 );
274 split-&gt;<a href="qwidget.html#show">show</a>();
275
276 // Set up the connection so that we can drop items into the icon view
277 QObject::<a href="qobject.html#connect">connect</a>(
278<a name="x2821"></a> iv, SIGNAL(<a href="qiconview.html#dropped">dropped</a>(QDropEvent*, const <a href="qvaluelist.html">QValueList</a>&lt;QIconDragItem&gt;&amp;)),
279 iv, SLOT(slotNewItem(QDropEvent*, const <a href="qvaluelist.html">QValueList</a>&lt;QIconDragItem&gt;&amp;)));
280
281 // Populate the QIconView with icons
282 DDIconViewItem *item;
283 item = new DDIconViewItem( iv, "Red", QPixmap( red_icon ) );
284 item-&gt;<a href="qiconviewitem.html#setRenameEnabled">setRenameEnabled</a>( TRUE );
285 item = new DDIconViewItem( iv, "Green", QPixmap( green_icon ) );
286 item-&gt;<a href="qiconviewitem.html#setRenameEnabled">setRenameEnabled</a>( TRUE );
287 item = new DDIconViewItem( iv, "Blue", QPixmap( blue_icon ) );
288 item-&gt;<a href="qiconviewitem.html#setRenameEnabled">setRenameEnabled</a>( TRUE );
289
290 return app.<a href="qapplication.html#exec">exec</a>();
291}
292
293
294</pre>
295
296<p> <p>See also <a href="examples.html">Examples</a>.
297
298<!-- eof -->
299<p><address><hr><div align=center>
300<table width=100% cellspacing=0 border=0><tr>
301<td>Copyright &copy; 2007
302<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
303<td align=right><div align=right>Qt 3.3.8</div>
304</table></div></address></body>
305</html>
Note: See TracBrowser for help on using the repository browser.