Qt learning path (52): One of the drag and drop Technologies

Source: Internet
Author: User

Drag and Drop, also known as DnD, is an essential technology in modern software development. It provides a mechanism for information exchange within an application or even between applications, and exchanges clipboard content between the operating system and the application, it can also be considered a part of DnD. DnD is actually composed of two parts: Drag and Drop. Drag is to Drag the Drag-and-Drop object, and Drop is to put the Drag-and-Drop object. The former is generally a process of pressing the mouse, while the latter is a process of releasing the mouse, the mouse is always pressed between the two. Of course, this is only a common situation. In other cases, it depends on the specific implementation of the application. For Qt, widgets can be either drag objects or drop objects, or both. The following example is from C ++ GUI Programming with Qt 4, 2nd Edition. In this example, we create a program that can drag and drop text files in the system and then read the content in the window. Mainwindow. h

 
 
  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3.  
  4. #include <QtGui>  
  5.  
  6. class MainWindow : public QMainWindow  
  7. {  
  8.     Q_OBJECT  
  9.  
  10. public:  
  11.     MainWindow(QWidget *parent = 0);  
  12.     ~MainWindow();  
  13.  
  14. protected:  
  15.     void dragEnterEvent(QDragEnterEvent *event);  
  16.     void dropEvent(QDropEvent *event);  
  17.  
  18. private:  
  19.     bool readFile(const QString &fileName);  
  20.     QTextEdit *textEdit;  
  21. };  
  22.  
  23. #endif // MAINWINDOW_H 
Mainwindow. cpp
 
 
  1. #include "mainwindow.h"  
  2.  
  3. MainWindow::MainWindow(QWidget *parent)  
  4.     : QMainWindow(parent)  
  5. {  
  6.     textEdit = new QTextEdit;  
  7.     setCentralWidget(textEdit);  
  8.  
  9.     textEdit->setAcceptDrops(false);  
  10.     setAcceptDrops(true);  
  11.  
  12.     setWindowTitle(tr("Text Editor"));  
  13. }  
  14.  
  15. MainWindow::~MainWindow()  
  16. {  
  17. }  
  18.  
  19. void MainWindow::dragEnterEvent(QDragEnterEvent *event)  
  20. {  
  21.     if (event->mimeData()->hasFormat("text/uri-list")) {  
  22.         event->acceptProposedAction();  
  23.     }  
  24. }  
  25.  
  26. void MainWindow::dropEvent(QDropEvent *event)  
  27. {  
  28.     QList<QUrl> urls = event->mimeData()->urls();  
  29.     if (urls.isEmpty()) {  
  30.         return;  
  31.     }  
  32.  
  33.     QString fileName = urls.first().toLocalFile();  
  34.     if (fileName.isEmpty()) {  
  35.         return;  
  36.     }  
  37.  
  38.     if (readFile(fileName)) {  
  39.         setWindowTitle(tr("%1 - %2").arg(fileName, tr("Drag File")));  
  40.     }  
  41. }  
  42.  
  43. bool MainWindow::readFile(const QString &fileName)  
  44. {  
  45.     bool r = false;  
  46.     QFile file(fileName);  
  47.     QTextStream in(&file);  
  48.     QString content;  
  49.     if(file.open(QIODevice::ReadOnly)) {  
  50.         in >> content;  
  51.         r = true;  
  52.     }  
  53.     textEdit->setText(content);  
  54.     return r;  
Main. cpp
 
 
  1. #include <QtGui/QApplication>  
  2. #include "mainwindow.h"  
  3.  
  4. int main(int argc, char *argv[])  
  5. {  
  6.     QApplication a(argc, argv);  
  7.     MainWindow w;  
  8.     w.show();  
  9.     return a.exec();  
The code here is not very complex. In MainWindow, A QTextEdit serves as a widget in the middle of the window. This class has two protected functions: dragEnterEvent () and dropEvent (). Both of these functions are inherited from QWidget. You can see that their names are two events, not just signal. In the constructor, we create the QTextEdit object. By default, QTextEdit can accept text type information dragged and dropped from other applications. If you drag a file to the file, the file name is inserted to the current position of the text. However, we want MainWindow to read the file content, not just insert the file name, so we have processed the drop event in MainWindow. Therefore, we need to set the setAcceptDrops () function of QTextEdit to false, set setAcceptDrops () of MainWindow to true, so that MainWindow can process the drop event. When you drag an object to a component, the dragEnterEvent () function is called back. If we call the acceptProposeAction () function in the event processing code, we can imply to the user that you can put the dragged object on this component. By default, components do not accept drag and drop. If such a function is called, Qt automatically uses the cursor to indicate whether the object can be placed on the component. Here, we want to tell the user that the window can be dragged and dropped. Therefore, we first check the MIME type of drag and drop. The MIME type is text/uri-list, which is usually used to describe a list of URIs. These Uris can be file names, URLs, or other resource descriptors. The MIME type is defined by Internet Assigned Numbers Authority (IANA). The Qt drag and drop event uses the MIME type to determine the type of the drag and drop object. For more information about MIME types, see http://www.iana.org/assignments/media-types/ . When you release an object to a component, the dropEvent () function is called back. We use QMimeData: urls () or a list of QUrl. In general, this type of drag should only use one file, but it is not excluded that multiple files are dragged together. Therefore, we need to check whether the list is empty. If it is not empty, the first list is taken out. If not, return immediately. Finally, we call the readFile () function to read the file content. Read operations will be described in detail in a later chapter, which will not be described here. Now our mini-program has been explained. Run it to see the effect! For drag and detach, Qt also provides similar functions: dragMoveEvent () and dragLeaveEvent (), but for most applications, the usage of these two functions is much lower.

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

Related Article

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.