Qt drag and drop Learning

Source: Internet
Author: User
To start the drag operation, you must:

Create a qdrag object
Call the exec () function of this object.
Time to start drag

In simple cases, directly start the drag operation in the mousepressevent
Generally, create a position flag in the mousepressevent to start the drag operation in the mousemoveevent.
Create a qdrag object
The drag and drop processes are actually a data transfer process. Where is the data stored? This is qmimedata.

Qdrag * drag = new qdrag (this); qmimedata * mimedata = new qmimedata; mimedata-> setdata (mimetype, data); drag-> setmimedata (mimedata, we may want to see a dragged object move with the mouse. Actually, this is an image that moves with the mouse.

Qpixmap pixmap (child-> size (); child-> render (& pixmap); qdrag * drag = new qdrag (this); drag-> setmimedata (mimedata ); drag-> setpixmap (pixmap); drag-> sethotspot (Hotspot); the object to be dragged (A qwidget) is directly render to an image, and then set the image to a drag object. What is the above hotspot? It is the cursor position corresponding to the point in the image.

Qmimedata is actually a struct list.

Struct qmimedatastruct {qstring format; qvariant data ;}; this list is a member function of qmimedatemedivate.

Void qmimedatemedivate: setdata (const qstring & format, const qvariant & Data) qvariant qmimedatemedivate: getdata (const qstring & format) the setdata/settext/sethtml/setimagedata provided by constqmimedata is implemented through the above function. For example:

Void qmimedata: settext (const qstring & text) {q_d (qmimedata); D-> setdata (qlatin1string ("text/plain"), text);} void qmimedata :: setdata (const qstring & mimetype, const qbytearray & Data) {q_d (qmimedata); D-> setdata (mimetype, qvariant (data);} calls the exec Member
Two exec member functions:

Qt: dropaction qdrag: exec (QT: dropactions supportedactions = QT: moveaction) Qt: dropaction qdrag: exec (QT: dropactions supportedactions, QT :: dropaction defaultdropaction) the former is implemented by calling the latter:

Qt: dropaction qdrag: exec (QT: dropactions supportedactions) {return exec (supportedactions, QT: ignoreaction);} Check the Code:

Qt: dropaction qdrag: exec (QT: dropactions supportedactions, QT: dropaction defaultdropaction) {q_d (qdrag); If (! D-> data) {qwarning ("qdrag: No mimedata set before starting the drag"); Return D-> executed_action;} qdragmanager * manager = qdragmanager: Self (); d-> defaultdropaction = QT: ignoreaction; D-> possible_actions = supportedactions; If (manager) {If (defaultdropaction = QT: ignoreaction) {If (supportedactions & QT :: moveaction) {d-> defaultdropaction = QT: moveaction;} else if (supportedactions & QT: copyaction) {d-> defaultdropaction = QT: copyaction ;} else if (supportedactions & QT: linkaction) {d-> defaultdropaction = QT: linkaction ;}} else {d-> defaultdropaction = defaultdropaction ;} d-> executed_action = Manager-> drag (this);} return D-> executed_action;} the default value of D-> executed_action is QT: ignoreaction. You can see that:

When defaultdropaction is QT: ignoreaction, the system first tries to convert it to move, copy, or link.

When the execution fails, the returned value is QT: ignoreaction.

To accept the drop action, first set the widget

Setacceptdrops (true); then, the override two member functions are required.

This event must be handled, otherwise drop is unavailable.

Void window: dragenterevent (qdragenterevent * event) {If (Event-> mimedata ()-> hasformat ("text/plain") Event-> acceptproposedaction ();} qdropevent
Let's take a look at the inheritance relationships of the three events:

Let's take a look at the differences between a qdropevent and a qevent (compare the constructor ):

Qevent: qevent (type): D (0), T (type), posted (false), random t (false), m_accept (true) {} qdropevent :: qdropevent (...) {... ignore ();} indicates that m_accept of a common qevent is true by default, while qdropevent is false by default. What does this mean?

You do not need to call accept () to process common qevents. You only need to call ignore () when necessary.
When processing qdropevnet, you must explicitly call accept () to accept the event; otherwise, it is equivalent to calling ignore ()
What is acceptproposedaction?
Check the source code:

Inline void acceptproposedaction () {drop_action = default_action; accept () ;}well, it is clear that it is equivalent to setting the default drop_action first, and then calling accept () for the event ()

In this way, it is not difficult to understand why the following statements are sometimes used.

If (Event-> source () = This) {event-> setdropaction (QT: moveaction); event-> Accept ();} else {event-> acceptproposedaction ();} One is the default action, the other is the move action, the corresponding drag startup command

Drag-> exec (QT: copyaction | QT: moveaction, QT: copyaction); reference



For your security, please only open the URL with reliable source
Cancel website opening
From: http://hi.baidu.com/cyclone/blog/item/081c9e2f248bf8281f308993.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.