Qt 拖放
来源:互联网 发布:大连软件职业学院 编辑:程序博客网 时间:2024/06/05 18:31
简述:
拖放 ,Drag and Drop,又称DnD,提供了一种能够在应用程序内部甚至应用程序之间信息交换的机制。Drag 是将被拖放对象“拖动”,Drop 是将被拖放对象“放下”,前者一般是一个按下鼠标的过程,而后者则是一个松开鼠标的过程,这两者之间鼠标一直是被按下的。拖放事件函数是一个筛选事件并放置的函数。实现需要分三步走:
1、设置接受拖放事件
首先需要设置setAcceptDrops(true)来接受放下事件
2、实现dragEnterEvent()
这是一个Protected函数,当用户将对象拖动到组件上面时,dragEnterEvent()函数会被回调。在dragEnterEvent()事件中对拖入的对象进行筛选,判断mimeData的类型是否是你能处理的,如果是,则调用event->acceptProposedAction()放行。
3、实现dropEvent()
拖放结束后会产生dropEvent()事件,在dropEvent()实现最后的放置操作。
4、实例
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTextEdit> 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_Hmainwindow.cpp
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { textEdit = new QTextEdit; setCentralWidget(textEdit); /*** * 默认情况下,QTextEdit 可以接受从其他的应用程序拖放过来的文本类型的信息。 * 如果用户把一个文件拖到这里面,那么就会把文件名插入到文本的当前位置。但是 * 我们希望让MainWindow 读取文件内容,而不仅仅是插入文件名,所以我们在 * MainWindow中对 drop 事件进行了处理,因此要把QTextEdit的setAcceptDrops() * 函数置为false,并且把MainWindow的setAcceptDrops()置为true, * 以便让MainWindow对 drop 事件进行处理。 **/ 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 <QApplication> #include "mainwindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
样例二:
只提供dragEnterEvent()函数和dropEvent()函数
void MainWindow::dragEnterEvent(QDragEnterEvent* event){ foreach(QUrl url, event->mimeData()->urls()){ QString filename = url.toLocalFile(); QFileInfo fi(filename); if (fi.suffix().toUpper() == "TXT") { event->acceptProposedAction(); return; } if (fi.fileName().toLower() == "debug.hdf") { event->acceptProposedAction(); return; } if (fi.fileName().toLower() == "fram_default_table.txt") { event->acceptProposedAction(); return; } }}void MainWindow::dropEvent(QDropEvent* event){ foreach(QUrl url, event->mimeData()->urls()) { QString filename = url.toLocalFile(); QFileInfo fi(filename); if (fi.suffix().toUpper() == "TXT") { loadFile(filename); break; } if (fi.fileName().toLower() == "debug.hdf") { QFile::copy(filename, QApplication::applicationDirPath() + "/debug.hdf"); QMessageBox::information(this, QApplication::applicationName(), "Debug file copied. Restart the application to activate debug session."); break; } if (fi.fileName().toLower() == "fram_default_table.txt") { QFile::copy(filename, QApplication::applicationDirPath() + "/fram_default_table.txt"); QMessageBox::information(this, QApplication::applicationName(), "Debug file copied. Restart the application to activate debug session."); break; } }}
1 1
- Qt拖放
- Qt拖放
- Qt 拖放
- Qt拖放
- Qt 拖放
- QT的拖放功能
- Qt中实现拖放
- Qt拖放技术之一
- qt的拖放
- Qt中的拖放问题
- Qt图片拖放
- QT 实现拖放功能
- Qt使用拖放
- Qt之拖放
- Qt文本拖放
- Qt之拖放
- Qt实现拖放操作
- Qt学习笔记-----拖放
- Appium自动化元素定位
- mysql分页原理和高效率的mysql分页查询语句
- 揭秘:北欧国家为何成为各路黑客的首要攻击目标?
- html入门(行级标签)
- HDU 1166 敌兵布阵 解析
- Qt 拖放
- web 3d 资源库大全
- 关于数据库的分表
- monkeyrunner简单使用及压力测试
- Mybatis Generator生成工具配置文件详解
- Javaweb备忘录2
- python3+PyQt5 实现理解python语法并做高亮显示的纯文本编辑器
- 本地通知
- property ,synthesize,dynamic