"Qt Learning Notes" 13. Drag-and-drop technology: Drag & Drop

Source: Internet
Author: User

1. Accept drag and drop


Drag & Drop is an interface operation for passing data between two windows.


Drag Source: Drag and drop Sources Window

Drag target: Drag and drop Destination window


Drag-and-drop operations:

1, in the source window: Select the target, press the mouse, move, drag to the target window (Drag)

2, in the target window: Cancel the mouse, to the specified position, release the mouse (Drop)

(Press ESC to cancel the operation)



Mime:

MIME (Multipurpose Internet Mail Extensions) The transmitted data is delivered in MIME format, which is a set of Type-data data: (TYPE0, DATA0) (Type1, Data1) (type2, data2) )  ... (Type-n, Data-n)

which

Type: String specifying the data type, for example: "Text/plain"

Data: An array of bytes, that is to be passed



To accept drag-and-drop steps:

Accept drag-and-drop setacceptdrops (TRUE);//override void Dragenterevent (qdragenterevent* event); void Dragleaveevent (qdragleaveevent* ev ENT), void Dragmoveevent (qdragmoveevent* event), void Dropevent (qdropevent* event);


Small exercise:

Implement a simple form that displays the dragged text content when the text is dragged into the form, and the label control in the body

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/8B/B7/wKiom1hWVL-g5xZRAAAKaw17nuo368.png "title=" 1.png " alt= "Wkiom1hwvl-g5xzraaakaw17nuo368.png"/>

Code:

#include <QDragEnterEvent> #include <QDragLeaveEvent> #include <QDragMoveEvent> #include < qdropevent> #include <QMimeData> #include "test13_1a_12_18.h" test13_1a_12_18::test13_1a_12_18 (Qwidget * Parent): Qmainwindow (parent) {UI.SETUPUI (this); Setacceptdrops (true);//settings: Accept drag-and-drop}//into void test13_1a_12_18:: Dragenterevent (qdragenterevent *event) {if (Event->mimedata ()->hasformat ("Text/plain")) {event-> Acceptproposedaction ();}} Release mouse void test13_1a_12_18::d ropevent (qdropevent *event) {if (Event->mimedata ()->hasformat ("Text/plain")) { QString text = Event->mimedata ()->text (); Ui.label->settext (text);}}


Ii. Initiation of drag and drop

In the Qwidget window, start a drag operation step:

1, the Mouse press

2, the mouse movement, when moving a distance, considered to be a drag operation. Can initiate a drag action


Implementation method:

1. Derive a subclass

2. Overriding the function:

virtual void Mousemoveevent (qmouseevent* event); virtual void Mousepressevent (qmouseevent* event);


Transfer multiple MIME data simultaneously:

mimedata->sethtml (  "xxxxxxx"  ); Mimedata->settext (  text );mimedata-> ("123,"  dat);      //Custom formatted Data//data :  Qbytearray 
Void mylistwidget::mousepressevent (qmouseevent *event) {//  Note the beginning m_dragposition = event- >pos (); M_dragitem = this->itemat (m_dragposition);//  Remember to call the parent class's function (keep the original keystroke behavior) Qlistwidget:: Mousemoveevent (event);} Void mylistwidget::mousemoveevent (qmouseevent *event) {//  only allow left-drag if  ( ! ( Event->buttons ()  & qt::leftbutton)  ) {return;}   move a certain distance before you start dragging if  ((Event->pos ()  - m_dragposition). Manhattanlength ()  <  Qapplication::startdragdistance ()) {return;}   Find dragged Item if  (!m_dragitem) {return;} Qstring text = m_dragitem->text ();//  Create Data Qdrag* drag = new qdrag ( this); Qmimedata* mimedata = new qmimedata;mimedata->settext (text);d Rag->setmimedata ( Mimedata);//  Start drag-and-drop &NBSP;START&NBSP;A&NBSP;DRAG&NBSP;QT::D ropaction result = drag->exec ( qt::copyaction | qt::moveaction);//  Check whether the operation was successful if  (qt::iGnoreaction != result) {}else{//  hold down the CTRL key: qt::copyaction//  do not press:  Qt::RemoveAction}} 


"Qt Learning Notes" 13. Drag-and-drop technology: Drag & Drop

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.