-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraggablelistwidget.cpp
48 lines (40 loc) · 1.5 KB
/
draggablelistwidget.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
// DraggableListWidget.cpp
#include "draggablelistwidget.h"
#include "pathwidgetitem.h"
#include "qevent.h"
#include <QFileInfo>
DraggableListWidget::DraggableListWidget(QWidget *parent) : QListWidget(parent) {
// Enable drag and drop
setAcceptDrops(true);
}
void DraggableListWidget::dragEnterEvent(QDragEnterEvent *event) {
// Accept the drag event if it contains URLs (file paths)
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
void DraggableListWidget::dragMoveEvent(QDragMoveEvent *event) {
// Accept the drag move event if it contains URLs
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
void DraggableListWidget::dropEvent(QDropEvent *event) {
const QMimeData *mimeData = event->mimeData();
// Check if the dropped data contains URLs
if (mimeData->hasUrls()) {
QList<QUrl> urlList = mimeData->urls();
// Process each URL
for (const QUrl &url : urlList) {
QFileInfo fileInfo(url.toLocalFile());
QString filePath = fileInfo.absoluteFilePath();
// Filter for .png, .jpg, .jpeg files
if (fileInfo.suffix().compare("png", Qt::CaseInsensitive) == 0 ||
fileInfo.suffix().compare("jpg", Qt::CaseInsensitive) == 0 ||
fileInfo.suffix().compare("jpeg", Qt::CaseInsensitive) == 0) {
// Add the file as a PathWidgetItem
new PathWidgetItem(filePath, this);
}
}
}
}