QtBase  v6.3.1
Signals | Public Member Functions | Static Public Member Functions | Friends | List of all members
QDrag Class Reference

The QDrag class provides support for MIME-based drag and drop data transfer. More...

#include <qdrag.h>

Inheritance diagram for QDrag:
Inheritance graph
[legend]
Collaboration diagram for QDrag:
Collaboration graph
[legend]

Signals

void actionChanged (Qt::DropAction action)
 
void targetChanged (QObject *newTarget)
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 

Public Member Functions

 QDrag (QObject *dragSource)
 
 ~QDrag ()
 
void setMimeData (QMimeData *data)
 
QMimeDatamimeData () const
 
void setPixmap (const QPixmap &)
 
QPixmap pixmap () const
 
void setHotSpot (const QPoint &hotspot)
 
QPoint hotSpot () const
 
QObjectsource () const
 
QObjecttarget () const
 
Qt::DropAction exec (Qt::DropActions supportedActions=Qt::MoveAction)
 
Qt::DropAction exec (Qt::DropActions supportedActions, Qt::DropAction defaultAction)
 
void setDragCursor (const QPixmap &cursor, Qt::DropAction action)
 
QPixmap dragCursor (Qt::DropAction action) const
 
Qt::DropActions supportedActions () const
 
Qt::DropAction defaultAction () const
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 
virtual ~QObject ()
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const
 
void setObjectName (const QString &name)
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 
bool isWindowType () const
 
bool signalsBlocked () const noexcept
 
bool blockSignals (bool b) noexcept
 
QThreadthread () const
 
void moveToThread (QThread *thread)
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 
template<typename T >
T findChild (const QString &aName=QString(), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<typename T >
QList< TfindChildren (const QString &aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<typename T >
QList< TfindChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
const QObjectListchildren () const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 
void dumpObjectInfo () const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 
bool inherits (const char *classname) const
 

Static Public Member Functions

static void cancel ()
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
static QMetaObject::Connection ::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot)
 
static QMetaObject::Connection ::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot)
 
template<typename Func1 , typename Func2 >
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 

Friends

class QDragManager
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Properties inherited from QObject
QString objectName
 the name of this object More...
 

Detailed Description

The QDrag class provides support for MIME-based drag and drop data transfer.

\inmodule QtGui

Drag and drop is an intuitive way for users to copy or move data around in an application, and is used in many desktop environments as a mechanism for copying data between applications. Drag and drop support in Qt is centered around the QDrag class that handles most of the details of a drag and drop operation.

The data to be transferred by the drag and drop operation is contained in a QMimeData object. This is specified with the setMimeData() function in the following way:

Note that setMimeData() assigns ownership of the QMimeData object to the QDrag object. The QDrag must be constructed on the heap with a parent QObject to ensure that Qt can clean up after the drag and drop operation has been completed.

A pixmap can be used to represent the data while the drag is in progress, and will move with the cursor to the drop target. This pixmap typically shows an icon that represents the MIME type of the data being transferred, but any pixmap can be set with setPixmap(). The cursor's hot spot can be given a position relative to the top-left corner of the pixmap with the setHotSpot() function. The following code positions the pixmap so that the cursor's hot spot points to the center of its bottom edge:

Note
On X11, the pixmap may not be able to keep up with the mouse movements if the hot spot causes the pixmap to be displayed directly under the cursor.

The source and target widgets can be found with source() and target(). These functions are often used to determine whether drag and drop operations started and finished at the same widget, so that special behavior can be implemented.

QDrag only deals with the drag and drop operation itself. It is up to the developer to decide when a drag operation begins, and how a QDrag object should be constructed and used. For a given widget, it is often necessary to reimplement \l{QWidget::mousePressEvent()}{mousePressEvent()} to determine whether the user has pressed a mouse button, and reimplement \l{QWidget::mouseMoveEvent()}{mouseMoveEvent()} to check whether a QDrag is required.

See also
{Drag and Drop}, QClipboard, QMimeData, {Draggable Icons Example}, {Draggable Text Example}, {Drop Site Example}, {Fridge Magnets Example}

Definition at line 57 of file qdrag.h.

Constructor & Destructor Documentation

◆ QDrag()

QDrag::QDrag ( QObject dragSource)
explicit

Constructs a new drag object for the widget specified by dragSource.

Definition at line 109 of file qdrag.cpp.

◆ ~QDrag()

QDrag::~QDrag ( )

Destroys the drag object.

Definition at line 125 of file qdrag.cpp.

Member Function Documentation

◆ actionChanged

void QDrag::actionChanged ( Qt::DropAction  action)
signal

This signal is emitted when the action associated with the drag changes.

See also
targetChanged()
Here is the caller graph for this function:

◆ cancel()

void QDrag::cancel ( )
static

Cancels a drag operation initiated by Qt.

Note
This is currently implemented on Windows and X11.
Since
5.7
See also
exec()

Definition at line 370 of file qdrag.cpp.

Here is the call graph for this function:

◆ defaultAction()

Qt::DropAction QDrag::defaultAction ( ) const

Returns the default proposed drop action for this drag operation.

See also
exec(), supportedActions()

Definition at line 356 of file qdrag.cpp.

Here is the caller graph for this function:

◆ dragCursor()

QPixmap QDrag::dragCursor ( Qt::DropAction  action) const

Returns the drag cursor for the action.

Since
5.0

Definition at line 313 of file qdrag.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ exec() [1/2]

Qt::DropAction QDrag::exec ( Qt::DropActions  supportedActions,
Qt::DropAction  defaultDropAction 
)
Since
4.3

Starts the drag and drop operation and returns a value indicating the requested drop action when it is completed. The drop actions that the user can choose from are specified in supportedActions.

The defaultDropAction determines which action will be proposed when the user performs a drag without using modifier keys.

{Note:} On Linux and \macos, the drag and drop operation can take some time, but this function does not block the event loop. Other events are still delivered to the application while the operation is performed. On Windows, the Qt event loop is blocked during the operation. However, QDrag::exec() on Windows causes processEvents() to be called frequently to keep the GUI responsive. If any loops or operations are called while a drag operation is active, it will block the drag operation.

Definition at line 259 of file qdrag.cpp.

Here is the call graph for this function:

◆ exec() [2/2]

Qt::DropAction QDrag::exec ( Qt::DropActions  supportedActions = Qt::MoveAction)
Since
4.3

Starts the drag and drop operation and returns a value indicating the requested drop action when it is completed. The drop actions that the user can choose from are specified in supportedActions. The default proposed action will be selected among the allowed actions in the following order: Move, Copy and Link.

{Note:} On Linux and \macos, the drag and drop operation can take some time, but this function does not block the event loop. Other events are still delivered to the application while the operation is performed. On Windows, the Qt event loop is blocked during the operation.

See also
cancel()

Definition at line 235 of file qdrag.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hotSpot()

QPoint QDrag::hotSpot ( ) const

Returns the position of the hot spot relative to the top-left corner of the cursor.

Definition at line 192 of file qdrag.cpp.

Here is the caller graph for this function:

◆ mimeData()

QMimeData * QDrag::mimeData ( ) const

Returns the MIME data that is encapsulated by the drag object.

Definition at line 148 of file qdrag.cpp.

Here is the caller graph for this function:

◆ pixmap()

QPixmap QDrag::pixmap ( ) const

Returns the pixmap used to represent the data in a drag and drop operation.

Definition at line 168 of file qdrag.cpp.

Here is the caller graph for this function:

◆ setDragCursor()

void QDrag::setDragCursor ( const QPixmap cursor,
Qt::DropAction  action 
)

Sets the drag cursor for the action. This allows you to override the default native cursors. To revert to using the native cursor for action pass in a null QPixmap as cursor.

Note: setting the drag cursor for IgnoreAction may not work on all platforms. X11 and macOS has been tested to work. Windows does not support it.

Definition at line 298 of file qdrag.cpp.

◆ setHotSpot()

void QDrag::setHotSpot ( const QPoint hotspot)

Sets the position of the hot spot relative to the top-left corner of the pixmap used to the point specified by hotspot.

{Note:} on X11, the pixmap may not be able to keep up with the mouse movements if the hot spot causes the pixmap to be displayed directly under the cursor.

Definition at line 182 of file qdrag.cpp.

Here is the caller graph for this function:

◆ setMimeData()

void QDrag::setMimeData ( QMimeData data)

Sets the data to be sent to the given MIME data. Ownership of the data is transferred to the QDrag object.

Definition at line 135 of file qdrag.cpp.

Here is the caller graph for this function:

◆ setPixmap()

void QDrag::setPixmap ( const QPixmap pixmap)

Sets pixmap as the pixmap used to represent the data in a drag and drop operation. You can only set a pixmap before the drag is started.

Definition at line 159 of file qdrag.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ source()

QObject * QDrag::source ( ) const

Returns the source of the drag object. This is the widget where the drag and drop operation originated.

Definition at line 202 of file qdrag.cpp.

Here is the caller graph for this function:

◆ supportedActions()

Qt::DropActions QDrag::supportedActions ( ) const

Returns the set of possible drop actions for this drag operation.

See also
exec(), defaultAction()

Definition at line 344 of file qdrag.cpp.

Here is the caller graph for this function:

◆ target()

QObject * QDrag::target ( ) const

Returns the target of the drag and drop operation. This is the widget where the drag object was dropped.

Definition at line 212 of file qdrag.cpp.

Here is the caller graph for this function:

◆ targetChanged

void QDrag::targetChanged ( QObject newTarget)
signal

This signal is emitted when the target of the drag and drop operation changes, with newTarget the new target.

See also
target(), actionChanged()
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ QDragManager

friend class QDragManager
friend

Definition at line 93 of file qdrag.h.


The documentation for this class was generated from the following files: