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
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include <QtGui>
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- MainWindow(QWidget *parent = 0);
- ~MainWindow();
-
- protected:
- void dragEnterEvent(QDragEnterEvent *event);
- void dropEvent(QDropEvent *event);
-
- private:
- bool readFile(const QString &fileName);
- QTextEdit *textEdit;
- };
-
- #endif // MAINWINDOW_H
Mainwindow. cpp
- #include "mainwindow.h"
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- textEdit = new QTextEdit;
- setCentralWidget(textEdit);
-
- textEdit->setAcceptDrops(false);
- setAcceptDrops(true);
-
- setWindowTitle(tr("Text Editor"));
- }
-
- MainWindow::~MainWindow()
- {
- }
-
- void MainWindow::dragEnterEvent(QDragEnterEvent *event)
- {
- if (event->mimeData()->hasFormat("text/uri-list")) {
- event->acceptProposedAction();
- }
- }
-
- void MainWindow::dropEvent(QDropEvent *event)
- {
- QList<QUrl> urls = event->mimeData()->urls();
- if (urls.isEmpty()) {
- return;
- }
-
- QString fileName = urls.first().toLocalFile();
- if (fileName.isEmpty()) {
- return;
- }
-
- if (readFile(fileName)) {
- setWindowTitle(tr("%1 - %2").arg(fileName, tr("Drag File")));
- }
- }
-
- bool MainWindow::readFile(const QString &fileName)
- {
- bool r = false;
- QFile file(fileName);
- QTextStream in(&file);
- QString content;
- if(file.open(QIODevice::ReadOnly)) {
- in >> content;
- r = true;
- }
- textEdit->setText(content);
- return r;
- }
Main. cpp
- #include <QtGui/QApplication>
- #include "mainwindow.h"
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- 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