Each fridge magnet is represented by an instance of the \c
DragLabel class:
- \snippet examples/draganddrop/fridgemagnets/draglabel.h 0
+ \snippet draganddrop/fridgemagnets/draglabel.h 0
Each instance of this QLabel subclass will be used to display an
pixmap generated from a text string. Since we cannot store both
In the \c DragLabel constructor, we first create a QImage object
on which we will draw the fridge magnet's text and frame:
- \snippet examples/draganddrop/fridgemagnets/draglabel.cpp 0
+ \snippet draganddrop/fridgemagnets/draglabel.cpp 0
Its size depends on the current font size, and its format is
QImage::Format_ARGB32_Premultiplied; i.e., the image is stored
simple lines to complex shapes like pies and chords. It can also
draw aligned text and pixmaps.
- \snippet examples/draganddrop/fridgemagnets/draglabel.cpp 1
+ \snippet draganddrop/fridgemagnets/draglabel.cpp 1
A painter can be activated by passing a paint device to the
constructor, or by using the \l{QPainter::}{begin()} method as we
flags argument is a bitwise-OR of the Qt::ImageConversionFlags;
passing 0 for flags sets all the default options).
- \snippet examples/draganddrop/fridgemagnets/draglabel.cpp 2
+ \snippet draganddrop/fridgemagnets/draglabel.cpp 2
Finally, we set the label's \l{QLabel::pixmap}{pixmap property}
and store the label's text for later use.
The \c DragWidget class inherits QWidget, providing support for
drag and drop operations:
- \snippet examples/draganddrop/fridgemagnets/dragwidget.h 0
+ \snippet draganddrop/fridgemagnets/dragwidget.h 0
To make the widget responsive to drag and drop operations, we simply
reimplement the \l{QWidget::}{dragEnterEvent()},
In the constructor, we first open the file containing the words on
our fridge magnets:
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 0
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 0
QFile is an I/O device for reading and writing text and binary
files and resources, and may be used by itself or in combination
to be read from the stream), we read one line at a time using
QTextStream's \l {QTextStream::}{readLine()} method.
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 1
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 1
For each line, we create a \c DragLabel object using the read line
as text, we calculate its position and ensure that it is visible by
We also set the \c FridgeMagnets widget's palette, minimum size
and window title.
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 2
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 2
Finally, to enable our user to move the fridge magnets around, we
must also set the \c FridgeMagnets widget's
\l{QWidget::acceptDrops}{acceptDrops} property.
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 3
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 3
Setting this property to true announces to the system that this
widget \e may be able to accept drop events (events that are sent
Let's take a look at the \l{QWidget::}{mousePressEvent()} event
handler, where drag and drop operations begin:
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 13
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 13
\snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 14
Mouse events occur when a mouse button is pressed or released
given MIME type. In our case, we associate our item data with the
custom \c application/x-fridgemagnet type.
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 15
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 15
Note that we also associate the magnet's text with the
\c text/plain MIME type using QMimeData's \l{QMimeData::}{setText()}
example, we simply use the pixmap used by the label itself to make
it look like the fridge magnet itself is being moved.
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 16
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 16
We also specify the cursor's hot spot, its position relative to the
top-level corner of the drag pixmap, to be the point we calculated
We start the drag operation using QDrag's \l{QDrag::}{exec()} function,
requesting that the magnet is copied when the drag is completed.
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 17
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 17
The function returns the drop action actually performed by the user
(this can be either a copy or a move action in this case); if this
In our \c dragEnterEvent() implementation, we first determine
whether we support the event's MIME type or not:
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 4
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 5
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 6
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 4
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 5
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 6
If the type is \c application/x-fridgemagnet and the event
origins from any of this application's fridge magnet widgets, we
origins from any other widget, we simply accept the proposed
action.
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 7
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 7
We also accept the proposed action if the event's MIME type is \c
text/plain, i.e., if QMimeData::hasText() returns true. If the
\l {QDragMoveEvent::}{ignore()} method allowing the event to be
propagated further.
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 8
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 8
Drag move events occur when the cursor enters a widget, when it
moves within the widget, and when a modifier key is pressed on the
data.
\target drop
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 9
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 9
The QMimeData class provides a container for data that
records information about its MIME type. QMimeData objects
MIME type using a data stream in order to create a new \c DragLabel
object.
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 10
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 10
The QDataStream class provides serialization of binary data to a
QIODevice (a data stream is a binary stream of encoded information
Finally, we create a label and move it to the event's position:
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 11
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 11
If the source of the event is also the widget receiving the
drop event, we set the event's drop action to Qt::MoveAction and
action. This lets the user drop selected text from a text editor or
Web browser onto the widget to add more fridge magnets.
- \snippet examples/draganddrop/fridgemagnets/dragwidget.cpp 12
+ \snippet draganddrop/fridgemagnets/dragwidget.cpp 12
If the event has any other type, we call the event's
\l{QDragMoveEvent::}{ignore()} method allowing the event to be