-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraganddropwidget.cpp
65 lines (50 loc) · 1.54 KB
/
draganddropwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "draganddropwidget.h"
DragAndDropWidget::DragAndDropWidget(QWidget *parent) : QWidget(parent)
{
setAcceptDrops(true);
QVBoxLayout *layout = new QVBoxLayout(this);
QLabel *ql = new QLabel("Drag and Drop here!",this);
layout->addWidget(ql);
}
DragAndDropWidget::~DragAndDropWidget()
{
}
void DragAndDropWidget::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("text/plain")){
qDebug() << "dragEnterEvent";
event->acceptProposedAction();
}
}
void DragAndDropWidget::dropEvent(QDropEvent *event)
{
const QMimeData* mimeData = event->mimeData();
// check for our needed mime type, here a file or a list of files
if (mimeData->hasUrls())
{
QStringList pathList;
QList<QUrl> urlList = mimeData->urls();
QString dbPath = urlList.first().toLocalFile();
if(fileCanBeOpened(dbPath)){
emit filePathRecived(dbPath);
}
}
}
void DragAndDropWidget::mousePressEvent(QMouseEvent *event)
{
qDebug() << "mousePressEvent";
QString dbPath = QFileDialog::getOpenFileName(nullptr,tr("Open file"), "/home", tr("Any files (*)"));
// ui->File1Path->setText(file1Name);
if(Utils::fileCanBeOpened(dbPath)){
emit filePathRecived(dbPath);
}
}
//bool DragAndDropWidget::fileCanBeOpened(const QString &path) const{
// QFile dbFile(path);
// dbFile.open(QIODevice::ReadOnly);
// if(!dbFile.isOpen()){
// qDebug() << "File can't be opened, path: " << path;
// return false;
// }
// return true;
//}