Qt learning path (53): Drag and Drop Technology 2

Source: Internet
Author: User
Qt learning path (53): Drag and Drop Technology 2

You have already read Original Works 7 times from A.M. on February 17,. you are allowed to repost the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://devbean.blog.51cto.com/448512/286796 has not been to write a blog for a long time, some time ago has been helping students get a spring-MVC project, today finally finished, but the company has to start to do flex 4, it may take some time! Next, the last time we talked about the drag and drop technology, today is still an example, also from C ++ GUI programming with QT 4, 2nd edition. This demo is more practical: the implementation of data drag between two lists. This requirement is common in many projects! The following is an example! Projectlistwidget. h

 
 
  1. #ifndef PROJECTLISTWIDGET_H  
  2. #define PROJECTLISTWIDGET_H  
  3.  
  4. #include <QtGui>  
  5.  
  6. class ProjectListWidget : public QListWidget  
  7. {  
  8.     Q_OBJECT  
  9.  
  10. public:  
  11.     ProjectListWidget(QWidget *parent = 0);  
  12.  
  13. protected:  
  14.     void mousePressEvent(QMouseEvent *event);  
  15.     void mouseMoveEvent(QMouseEvent *event);  
  16.     void dragEnterEvent(QDragEnterEvent *event);  
  17.     void dragMoveEvent(QDragMoveEvent *event);  
  18.     void dropEvent(QDropEvent *event);  
  19.  
  20. private:  
  21.     void performDrag();  
  22.  
  23.     QPoint startPos;  
  24. };  
  25.  
  26. #endif // PROJECTLISTWIDGET_H 
Projectlistwidget. cpp
 
 
  1. #include "projectlistwidget.h"  
  2.  
  3. ProjectListWidget::ProjectListWidget(QWidget *parent)  
  4.     : QListWidget(parent)  
  5. {  
  6.     setAcceptDrops(true);  
  7. }  
  8.  
  9. void ProjectListWidget::mousePressEvent(QMouseEvent *event)  
  10. {  
  11.     if (event->button() == Qt::LeftButton)  
  12.         startPos = event->pos();  
  13.     QListWidget::mousePressEvent(event);  
  14. }  
  15.  
  16. void ProjectListWidget::mouseMoveEvent(QMouseEvent *event)  
  17. {  
  18.     if (event->buttons() & Qt::LeftButton) {  
  19.         int distance = (event->pos() - startPos).manhattanLength();  
  20.         if (distance >= QApplication::startDragDistance())  
  21.             performDrag();  
  22.     }  
  23.     QListWidget::mouseMoveEvent(event);  
  24. }  
  25.  
  26. void ProjectListWidget::performDrag()  
  27. {  
  28.     QListWidgetItem *item = currentItem();  
  29.     if (item) {  
  30.         QMimeData *mimeData = new QMimeData;  
  31.         mimeData->setText(item->text());  
  32.  
  33.         QDrag *drag = new QDrag(this);  
  34.         drag->setMimeData(mimeData);  
  35.         drag->setPixmap(QPixmap(":/images/person.png"));  
  36.         if (drag->exec(Qt::MoveAction) == Qt::MoveAction)  
  37.             delete item;  
  38.     }  
  39. }  
  40.  
  41. void ProjectListWidget::dragEnterEvent(QDragEnterEvent *event)  
  42. {  
  43.     ProjectListWidget *source =  
  44.             qobject_cast<ProjectListWidget *>(event->source());  
  45.     if (source && source != this) {  
  46.         event->setDropAction(Qt::MoveAction);  
  47.         event->accept();  
  48.     }  
  49. }  
  50.  
  51. void ProjectListWidget::dragMoveEvent(QDragMoveEvent *event)  
  52. {  
  53.     ProjectListWidget *source =  
  54.             qobject_cast<ProjectListWidget *>(event->source());  
  55.     if (source && source != this) {  
  56.         event->setDropAction(Qt::MoveAction);  
  57.         event->accept();  
  58.     }  
  59. }  
  60.  
  61. void ProjectListWidget::dropEvent(QDropEvent *event)  
  62. {  
  63.     ProjectListWidget *source =  
  64.             qobject_cast<ProjectListWidget *>(event->source());  
  65.     if (source && source != this) {  
  66.         addItem(event->mimeData()->text());  
  67.         event->setDropAction(Qt::MoveAction);  
  68.         event->accept();  
  69.     }  
  70. }  
We can see from the constructor. Many components in QT can be dragged and dropped, but the default actions are not allowed. Therefore, in the constructor, we call setacceptdrops (true). This function allows the component to accept drag and drop events. In the mousepressevent () function, click the left mouse button and record the current position. It should be noted that this function needs to call the built-in processing functions of the system in order to implement the common operation. This should be noted in some rewrite event functions! Then we overwrite the mousemoveevent () event. Next let's take a look at the Code:
 
 
  1. void ProjectListWidget::mouseMoveEvent(QMouseEvent *event)  
  2. {  
  3.     if (event->buttons() & Qt::LeftButton) {  
  4.         int distance = (event->pos() - startPos).manhattanLength();  
  5.         if (distance >= QApplication::startDragDistance())  
  6.             performDrag();  
  7.     }  
  8.     QListWidget::mouseMoveEvent(event);  
Here we can determine that if you hold down the left button while dragging the mouse (that is, the content in the IF), a manhattanlength () value will be calculated. Literally, this is a "Manhattan length ". What does this mean? Let's take a look at what event. pos ()-startpos is. Remember that in the mousepressevent () function, we record the coordinates as startpos, and event. POs () is the current coordinate of the mouse: one point minus another. That's right. This is the vector! In fact, the so-called distance from Manhattan is the distance between two points (as to why this strange name is called, you will know it after checking encyclopedia !), That is, the length of the vector. The following is another judgment. If it is greater than qapplication: startdragdistance (), we will perform the drag operation. Of course, you still need to call the default mouse drag function. The significance of this judgment is to prevent the user from dragging the mouse due to factors such as hand jitter. The user must drag the mouse for a certain distance before we think that he wants to perform the drag operation, and this distance is provided by qapplication: startdragdistance (), which is usually 4px. Mongomdrag () starts the drag-and-drop process. We created a qdrag object and used this as the parent. Qdrag uses qmimedata to store data. For example, we use the qmimedata: settext () function to store data of the text/plain type as a string. Qmimedata provides many functions for storing data of URL and color types. You can use qdrag: setpixmap () to set the mouse style when dragging occurs. Qdrag: exec () blocks the drag operation until the user completes the operation or cancels the operation. It accepts different types of actions as parameters, and the return value is the actually executed action. These actions are of the QT: copyaction, QT: moveaction, and QT: linkaction types. The return value has these three actions, and a QT: ignoreaction is added to indicate that the user has canceled the drag and drop operation. These actions depend on the types allowed by the drag-and-drop source object, the types accepted by the target object, and the keyboard keys that are pressed during the drag-and-drop. After exec () is called, QT will delete the drag-and-drop object when it is not needed. Projectlistwidget can not only send drag events, but also accept data of different projectlistwidget objects in the same application. In dragenterevent (), we use Event-> source () to obtain such an object: If the drag-and-drop data comes from an object of the same type and comes from the same application, its pointer is returned, otherwise, null is returned. We use the qobject_cast macro to convert the pointer to the projectlistwidget * type, and then set to accept the drag of the QT: moveaction type. Dragmoveevent () has the same code as this function, because we need to rewrite the code for dragging and moving. Finally, in the dropevent () function, we retrieve the mimedata data in qdrag and call additem () to add it to the current list. In this way, a relatively complete drag-and-drop code is complete. The drag-and-drop technology is a powerful technology in QT, but it may be enough to simply implement the mouse event in a single component that does not involve data. You should consider it as needed!

This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/286796

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.