For the completeness of the answer: you need to subclass QLineEdit (create MyLineEdit class, which inherits from QLineEdit), and replace usage of QLineEdit with MyLineEdit (usually inside xxxx.ui file):
Create file: MyLineEdit.h:
#include <QLineEdit>
class MyLineEdit : public QLineEdit
{
public:
MyLineEdit(QWidget *parent = 0);
~MyLineEdit();
void dragEnterEvent(QDragEnterEvent *e);
void dragMoveEvent(QDragMoveEvent *e);
void dropEvent(QDropEvent *e);
};
Create file: MyLineEdit.cpp:
#include "MyLineEdit.h"
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QUrl>
#include <QFileInfo>
MyLineEdit::MyLineEdit(QWidget *parent)
: QLineEdit(parent)
{}
MyLineEdit::~MyLineEdit()
{}
void MyLineEdit::dragEnterEvent(QDragEnterEvent *e){
if (e->mimeData()->hasUrls()) {
e->acceptProposedAction();
}
}
void MyLineEdit::dragMoveEvent(QDragMoveEvent *e){
if (e->mimeData()->hasUrls()) {
e->acceptProposedAction();
}
}
void MyLineEdit::dropEvent(QDropEvent *event){
const QMimeData* mimeData = event->mimeData();
if (mimeData->hasUrls())
{
QStringList pathList;
QList<QUrl> urlList = mimeData->urls();
for (int i = 0; i < urlList.size() && i < 32; ++i)
{
QString str = urlList.at(i).toLocalFile();
if (!str.isEmpty())
{
// support txt/csv files only
QString suffix = QFileInfo(str).suffix();
if (suffix == "txt" || suffix == "csv") // place here whatever suffix you want
{
// action - set the file path field to the name of the dropped file
this->setText(str);
event->acceptProposedAction();
break;
}
}
}
}
}
And change the usage, for example, from this:
<widget class="QLineEdit" name="lineEdit_xxxx">
to this:
<widget class="MyLineEdit" name="lineEdit_xxxx">