之前一直跟着霍亚飞的《Qt Creator 快速入门 》学习Qt,但拖放事件程序一直没有成功运行,后来又搜集了很多资料,终于解决了问题。不说了,直接上代码。
1.建立工程Qt 工程(我的是QT5.8.0)myDragDrop。
new Project->Application->Qt Widgets Application->Choose,
Name->myDragDrop
Next:Desktop Qt 5.8.0 MinGW 32bit
Next:Class name->myMainWindow,Base class->QMainWindow,选择Genarate form,其他默认。
可以选择Genarate form 生成Gui,也可以不选择。在这里我们介绍Qt Gui和纯代码两种方法。直接在代码中说明。
2.首先是mymainwindow.h文件
#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H
#include <QMainWindow>
//#include <QTextEdit> // 2.纯代码时使用
namespace Ui {
class myMainWindow;
}
class myMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit myMainWindow(QWidget *parent = 0);
~myMainWindow();
private:
Ui::myMainWindow *ui;
//QTextEdit *textEdit; //2.纯代码时使用
protected:
void dragEnterEvent(QDragEnterEvent *event); //拖动进入事件
void dropEvent(QDropEvent *event); //放下事件
};
#endif // MYMAINWINDOW_H
3.mymainwindow.cpp文件
(1)Qt Gui需要在mymainwindow.ui添加Text Edit部件,然后左键点击myMainWindow这个大框,按下快捷键Ctrl+G使用网格布局(Lay Out in a Grid),确保运行之后的行编辑器能跟随窗口进行放大缩小。
(2)纯代码则不需要添加任何部件。
#include "mymainwindow.h"
#include "ui_mymainwindow.h"
#include <QDragEnterEvent>
#include <QUrl>
#include <QFile>
#include <QMimeData>
#include <QTextStream>
myMainWindow::myMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::myMainWindow)
{
ui->setupUi(this);
//1.Qt Gui使用。缺少则不能读取文件
ui->textEdit->setAcceptDrops(false); //禁用textEdit控件的放下操作
setAcceptDrops(true); //启用textEdit控件的放下操作
//2.纯代码时使用-------------------------
//textEdit = new QTextEdit;
//setCentralWidget(textEdit);
//缺少则不能读取文件
//textEdit->setAcceptDrops(false); //禁用textEdit控件的放下操作
//setAcceptDrops(true); //启用textEdit控件的放下操作
//-------------------------------------
setWindowTitle(tr("Text Editor"));
}
myMainWindow::~myMainWindow()
{
delete ui;
}
/*拖放分为拖动(Drag)和放下(Drop)两种操作。数据拖动时会被存储为MIME(Multipurpose-
* Internet Mail Extensions)类型,在Qt使用QMimeData类来表示MIME类型数据,并还用
* QDrag类来完成数据的转换,而整个拖放操作都是在几个鼠标事件和拖放事件中完成.
*/
void myMainWindow::dragEnterEvent(QDragEnterEvent *event)//拖动事件
{
//可打开.txt,.h,.cpp文件
if(event->mimeData()->hasUrls()) //数据中是否包含URL
{
event->acceptProposedAction(); //如果是则接受动作
}
else event->ignore();
}
void myMainWindow::dropEvent(QDropEvent *event)//放下事件
{
const QMimeData *mineData=event->mimeData();//获取MIME数据
if(mineData->hasUrls()) //如数据中包含URL
{
QList<QUrl>urlList=mineData->urls(); //获取URL列表
if(urlList.isEmpty())return ;
//将其中第一个URL表示为本地文件路径
QString fileName=urlList.at(0).toLocalFile();
if(fileName.isEmpty())return;
if(!fileName.isEmpty()) //若文件路径不为空
{
QFile file(fileName); //建立QFile对象并只读方式打开该文件
//窗口标题更改为显示文件路径
setWindowTitle(tr("%1 - %2").arg(fileName).arg("DND File"));
if(!file.open(QIODevice::ReadOnly)) return;
QTextStream in(&file); //建立文本流对象
ui->textEdit->setText(in.readAll()); //1.纯代码时使用。将文件所有内容读入编辑器
//textEdit->setText(in.readAll()); //2.纯代码时使用。将文件所有内容读入编辑器
}
}
}
4.main.cpp文件。没有作任何改变
#include "mymainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
myMainWindow w;
w.show();
return a.exec();
}
5.程序运行效果如动图。读取.txt文件中文没有问题,但读取.h和.cpp文件时中文显示乱码。解决后会持续更新,也欢迎各位朋友提供解决方案,谢谢!