add:添加文件路径支持拖入。
This commit is contained in:
36
src/QCustomQLineEdit.cpp
Normal file
36
src/QCustomQLineEdit.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "QCustomQLineEdit.h"
|
||||
|
||||
QCustomQLineEdit::QCustomQLineEdit(QWidget* parent) : QLineEdit(parent)
|
||||
{
|
||||
setAcceptDrops(true);
|
||||
}
|
||||
|
||||
void QCustomQLineEdit::dragEnterEvent(QDragEnterEvent* event)
|
||||
{
|
||||
if (event->mimeData()->hasUrls()) {
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
QLineEdit::dragEnterEvent(event);
|
||||
}
|
||||
|
||||
void QCustomQLineEdit::dragMoveEvent(QDragMoveEvent* event)
|
||||
{
|
||||
}
|
||||
|
||||
void QCustomQLineEdit::dropEvent(QDropEvent* event)
|
||||
{
|
||||
const QMimeData* mimeData = event->mimeData();
|
||||
if (mimeData->hasUrls()) {
|
||||
QList<QUrl> urls = mimeData->urls();
|
||||
if (urls.size() > 0) {
|
||||
QString file_name = urls.at(0).toLocalFile();
|
||||
setText(file_name);
|
||||
event->accept();
|
||||
}
|
||||
}
|
||||
QLineEdit::dropEvent(event);
|
||||
}
|
||||
|
||||
QCustomQLineEdit::~QCustomQLineEdit()
|
||||
{
|
||||
}
|
||||
27
src/QCustomQLineEdit.h
Normal file
27
src/QCustomQLineEdit.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef QCUSTOMQLINE
|
||||
#define QCUSTOMQLINE
|
||||
|
||||
#include <QLineEdit>
|
||||
|
||||
#include <QDropEvent>
|
||||
#include <QMimeData>
|
||||
#include <QDragEnterEvent>
|
||||
|
||||
class QCustomQLineEdit : public QLineEdit
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QCustomQLineEdit(QWidget* parent = nullptr);
|
||||
~QCustomQLineEdit() override;
|
||||
|
||||
protected:
|
||||
// 拖动文件到窗口 触发
|
||||
void dragEnterEvent(QDragEnterEvent* event);
|
||||
// 拖动文件到窗口移动文件 触发
|
||||
void dragMoveEvent(QDragMoveEvent* event);
|
||||
// 拖动文件到窗口释放文件触发
|
||||
void dropEvent(QDropEvent* event);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user