QT Drag and drop

Source: Internet
Author: User

qt Drag and drop (drag and drop)drag-and-drop is a way of data passing within an application or between applications. It is usually to provide replication and movement of data. Drag and drop mainly include drag and drop. Some windows may only need to provide drag functionality, some Windows may only need to drop, some windows may need both functions. Some of the QT controls do not need to be set up and have drag-and-drop functionality, such as Qlineedit, Qtextedit. Qlineedit can accept a dragged string, and qtextedit can accept the dragged string and the path of the dragged file. All you need to do is create an object and then display it as follows: Qlineedit lineEdit;
Lineedit.show ();
Qtextedit TextEdit;
Textedit.show ();
Qlineedit Accept text drag and drop, test effect:

Qtextedit Accept file drag and drop, display the path of the file, support multiple files Drag and drop, test effect:

Qwidget Implement drag and drop, you need to set accept drag and drop,Setacceptdrops (true);
and implement the following events:void Dragenterevent (Qdragenterevent *event);
void Dragmoveevent (Qdragmoveevent *event);
void Dropevent (Qdropevent *event);
void Mousepressevent (Qmouseevent *event);
void Mousemoveevent (Qmouseevent *event);
if we only need qwidget accept down, then there is no need to implement:void Mousepressevent (Qmouseevent *event);
void mousemoveevent (qmouseevent *event );These two events are mainly needed when we need qwidget to implement the drag. Qapplication provides two ways to start a drag operation:
qapplication::startdragdistance (): By the distance of the mouse movement to determine whether to start drag and drop, the default is 10 pixels. However, you can also use the static void setstartdragdistance (int l) to customize the distance you want to start dragging and dropping. Qapplication::startdragtime (): By the mouse click on the length of time to determine whether to start drag and drop, the default is 500ms. The static void Setstartdragtime (int ms) can also be used to customize the duration of the press. in order to implement drag and drop, we need toMousepressevent record the mouse-pressed coordinate point or point in time, and thenMousemoveevent Determines whether the distance of the mouse movement or the pressing time satisfies the drag-and-drop requirement, which initiates the drag-and-drop.
To perform a drag-and-drop operation, we need to create a Qdrag object that contains Qmimedata, which holds the data we need to drag. Example:
void Qdragdrop::mousepressevent (Qmouseevent *event) {__super::mousepressevent (event); mstartpoint = Event->pos () ;//Start}void qdragdrop::mousemoveevent (qmouseevent *event) {__super::mousemoveevent (event); if (Event->pos ()- Mstartpoint). Manhattanlength () > Qapplication::startdragdistance ())//Determine whether to perform drag {Qdrag *drag = new Qdrag (this); Qmimedata *mimedata = new Qmimedata; Qbytearray bytearray;//stores Data Qdatastream stream (&bytearray,qiodevice::writeonly);//stream, write data stream << qimage (" C:\\users\\dlp\\desktop\\2.png ");//Read the data of a picture Mimedata->setdata (" MyImage ", byteArray);//Set Data drag-> Setmimedata (mimedata);//Set Data drag->exec (qt::moveaction);//Perform drag delete drag;}}
in the example above, the data that reads a picture is stored in Qmimedata as the data to be passed. and the custom format "MyImage" is set. The effect is as follows:
The sample code is as follows:
#ifndef qdragdrop_h#define qdragdrop_h#include <QtWidgets/QWidget> #include <QMouseEvent> #include < Qhboxlayout>class qdragdrop:public qwidget{q_objectpublic:qdragdrop (qwidget *parent = 0); ~QDragDrop ();p rotected: void Dragenterevent (qdragenterevent *event); void Dragmoveevent (qdragmoveevent *event); void Dropevent (QDropEvent * event), void Mousepressevent (Qmouseevent *event), void Mousemoveevent (qmouseevent *event);p rivate:qpoint mStartPoint; Qhboxlayout  *m_hbox;}; #endif//Qdragdrop_h
#include "qdragdrop.h" #include <QApplication> #include <QDrag> #include <QMimeData> #include < Qlabel>qdragdrop::qdragdrop (Qwidget *parent): Qwidget (parent) {mstartpoint = qpoint (0, 0); m_hbox = new Qhboxlayout ( this); Setacceptdrops (true); Setwindowtitle ("Qwidget");} Qdragdrop::~qdragdrop () {}void qdragdrop::mousepressevent (qmouseevent *event) {__super::mousepressevent (event); Mstartpoint = Event->pos ();//Start}void qdragdrop::mousemoveevent (qmouseevent *event) {__super::mousemoveevent ( Event), if ((Event->pos ()-Mstartpoint). Manhattanlength () > Qapplication::startdragdistance ())//Determine whether to perform drag { Qdrag *drag = new Qdrag (this); Qmimedata *mimedata = new Qmimedata; Qbytearray bytearray;//stores Data Qdatastream stream (&bytearray,qiodevice::writeonly);//stream, write data stream << qimage (" C:\\users\\dlp\\desktop\\2.png ");//Read the data of a picture Mimedata->setdata (" MyImage ", byteArray);//Set Data drag-> Setmimedata (mimedata);//Set Data drag->exec (qt::moveaction);//Perform drag delete drag;}} void Qdragdrop::d ragentereVent (qdragenterevent *event) {__super::d ragenterevent (event); if (Event->mimedata ()->hasformat ("MyImage")) Event->acceptproposedaction ();} void Qdragdrop::d ragmoveevent (qdragmoveevent *event) {__super::d ragmoveevent (event); if (Event->mimedata () Hasformat ("MyImage")) event->acceptproposedaction ();} void Qdragdrop::d ropevent (qdropevent *event) {__super::d ropevent (event), if (Event->mimedata ()->hasformat (" MyImage ")) {Qbytearray ByteArray = Event->mimedata ()->data (" MyImage ");//Fetch Data Qdatastream stream (&bytearray, QIODEVICE::READONLY);//stream, read data qimage image;stream >> image; Qlabel *plabel = new Qlabel (this);p Label->setpixmap (qpixmap::fromimage (image)); M_hbox->addwidget (PLabel);}}
if we just need to receive down and do not need to drag, then we do not need the above example of the two mouse events. The following implements an example of dragging a picture to a window and using Qlabel to display it:Effect:
Example code:
#ifndef qshowimage_h#define qshowimage_h#include <QWidget> #include <QHBoxLayout> #include < qdragenterevent> #include <qmimedata>class qshowimage:public qwidget{q_objectpublic:qshowimage (QWidget * parent = 0); ~qshowimage ();p rotected:void dragenterevent (qdragenterevent *event); void Dropevent (Qdropevent *event); Private:qhboxlayout *mlayout;}; #endif//Qshowimage_h
#include "QShowImage.h" #include <QLabel> #include <qdebug>qshowimage::qshowimage (qwidget *parent): Qwidget (parent) {mlayout = new qhboxlayout (this); Setacceptdrops (true); Setwindowtitle ("ShowImage");} Qshowimage::~qshowimage () {}void qshowimage::d ragenterevent (qdragenterevent *event) {__super::d ragenterevent (Event ); if (Event->mimedata ()->hasurls ()) event->acceptproposedaction ();} void Qshowimage::d ropevent (qdropevent *event) {__super::d ropevent (event); if (Event->mimedata ()->hasurls ()) { qlist<qurl> urls = Event->mimedata ()->urls (); For each (qurl URL in URLs) {Qlabel *plabel = new Qlabel (this); Qimage image (Url.tostring (). Mid (8));p Label->setpixmap (qpixmap::fromimage (image)); Mlayout->addwidget ( Plabel);}}}

for Qlistwidget, drag and drop is not as complex as the above, just set up, you can implement Qlistwidgetitem drag:Setdragenabled (TRUE);

Implementation code:
Qlistwidget Qlistwidget; Qstringlist strlist = {"Item1", "Item2", "Item3", "Item4", "ITEM5"};qlistwidget.additems (strlist); Qlistwidget.setviewmode (Qlistview::iconmode); qlistwidget.setdragenabled (true); Qlistwidget.show ();
But the above only moves the position, the item's specific index has not changed. And if we need to implement other drag-and-drop capabilities, it does not meet our needs. Then we need to re-implement the drag-and-drop event to customize the functionality we need to implement. I implemented the ability to drag item and then add an item, two Qlistwidgetitem is not related, just to show how to implement it. Effect:
Example code:
#ifndef listwidget_h#define listwidget_h#include <QListWidget> #include <qmouseevent>class listwidget: Public Qlistwidget{q_objectpublic:listwidget (Qwidget *parent = 0); ~listwidget (); void Dragenterevent (QDragEnterEvent *event), void Dragmoveevent (Qdragmoveevent *event), void Dropevent (qdropevent *event);p rivate:}; #endif//Listwidget_h
#include "ListWidget.h" #include <QDrag> #include <QMimeData> #include <qlabel>listwidget:: Listwidget (Qwidget *parent): Qlistwidget (parent) {Qstringlist strlist = {"Item1", "Item2", "Item3", "Item4", "ITEM5"};ad Ditems (strlist); Setviewmode (Qlistview::iconmode); setdragenabled (true); Setwindowtitle ("Qlistwidget");} Listwidget::~listwidget () {}void listwidget::d ragenterevent (qdragenterevent *event) {__super::d ragenterevent (Event ); Listwidget *plist = Qobject_cast<listwidget *> (Event->source ());//Drag the source object if (pList && pList = = this) { Event->setdropaction (Qt::D ropaction::copyaction);//Set drag action, affect mouse cursor event->accept ();}} void Listwidget::d ragmoveevent (qdragmoveevent *event) {__super::d ragmoveevent (event); Listwidget *plist = Qobject_cast<listwidget *> (Event->source ());//Drag the source object if (pList && pList = = this) { Event->setdropaction (Qt::D ropaction::copyaction);//Set drag action, affect mouse cursor event->accept ();}} void Listwidget::d ropevent (qdropevent *event) {__super::d ropevent(event); Listwidget *plist = Qobject_cast<listwidget *> (Event->source ());//Drag the source object if (pList && pList = = this) { Qlistwidgetitem *pitem = new Qlistwidgetitem (this);//After performing the release, Do Something Pitem->settext (QString ("Item"). Append (QString:: Number (count ()))); AddItem (Pitem);}}
AC qq:1245178753This address: http://blog.csdn.net/u011417605/article/details/51316037source Download: http://download.csdn.net/detail/u011417605/9509997

QT Drag and 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.