-
Notifications
You must be signed in to change notification settings - Fork 0
/
droppablelineedit.cpp
53 lines (48 loc) · 1.4 KB
/
droppablelineedit.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
#include <QtWidgets>
#include "droppablelineedit.h"
DroppableLineEdit::DroppableLineEdit(QWidget *parent)
: QLineEdit(parent)
{
setAcceptDrops(true);
}
void DroppableLineEdit::dragEnterEvent(QDragEnterEvent *event)
{
// accept just text/uri-list and text/plain mime formats
if ( (event->mimeData()->hasFormat("text/uri-list")) ||
(event->mimeData()->hasFormat("text/plain")) )
{
event->acceptProposedAction();
}
}
void DroppableLineEdit::dropEvent(QDropEvent *event)
{
QList<QUrl> urlList;
QString fName;
QFileInfo info;
const QMimeData *data = event->mimeData();
if (data->hasUrls())
{
urlList = data->urls(); // returns list of QUrls
// if just text was dropped, urlList is empty (size == 0)
if ( urlList.size() > 0) // if at least one QUrl is present in list
{
fName = urlList[0].toLocalFile(); // convert first QUrl to local path
info.setFile( fName ); // information about file
if ( info.isFile() )
{
setText( fName ); // if is file, setText
event->acceptProposedAction();
} else {
// setText("has url but Cannot drop");
event->ignore();
}
}
}
else if (data->hasText())
{
setText(data->text());
event->acceptProposedAction();
} else {
event->ignore();
}
}