#ifndef TEXTEDIT_H
#define TEXTEDIT_H
#include <QTextEdit>
class TextEdit : public QTextEdit
{
Q_OBJECT
public:
explicit TextEdit(QWidget *parent = 0);
protected:
void dragEnterEvent(QDragEnterEvent *);
void dropEvent(QDropEvent *);
private:
bool readFile(const QString &filename);
};
#endif // TEXTEDIT_H
#include "textedit.h"
#include <QtWidgets>
TextEdit::TextEdit(QWidget *parent) :
QTextEdit(parent)
{
}
void TextEdit::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("text/uri-list")) {
event->acceptProposedAction();
}
}
void TextEdit::dropEvent(QDropEvent *event)
{
QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty()) {
qDebug() << "dropEvent urls";
return;
}
QString filename = urls.first().toLocalFile();
if (filename.isEmpty()) {
qDebug() << "dropEvent filename:" << filename;
return;
}
readFile(filename);
}
bool TextEdit::readFile(const QString &filename)
{
QFile file(filename);
if (file.open(QFile::ReadOnly)) {
QTextStream stream(&file);
this->setText(stream.readAll());
return true;
}
return false;
}
#ifndef LISTWIDGET_H
#define LISTWIDGET_H
#include <QListWidget>
class ListWidget : public QListWidget
{
Q_OBJECT
public:
explicit ListWidget(QWidget *parent = 0);
protected:
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void dragEnterEvent(QDragEnterEvent *);
void dragMoveEvent(QDragMoveEvent *);
void dropEvent(QDropEvent *);
private:
void performDrag();
QPoint startPos_;
};
#endif // LISTWIDGET_H
#include "listwidget.h"
#include <QtWidgets>
ListWidget::ListWidget(QWidget *parent) :
QListWidget(parent)
{
this->setAcceptDrops(true);
}
void ListWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
startPos_ = event->pos();
}
QListWidget::mousePressEvent(event);
}
void ListWidget::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
int distance = (event->pos() - startPos_).manhattanLength();
if (distance >= QApplication::startDragDistance()) {
performDrag();
}
}
QListWidget::mouseMoveEvent(event);
}
void ListWidget::dragEnterEvent(QDragEnterEvent *event)
{
ListWidget *source = qobject_cast<ListWidget*>(event->source());
if (source && source != this) {
event->setDropAction(Qt::MoveAction);
event->accept();
}
}
void ListWidget::dragMoveEvent(QDragMoveEvent *event)
{
ListWidget *source = qobject_cast<ListWidget*>(event->source());
if (source && source != this) {
event->setDropAction(Qt::MoveAction);
event->accept();
}
}
void ListWidget::dropEvent(QDropEvent *event)
{
ListWidget *source = qobject_cast<ListWidget*>(event->source());
if (source && source != this) {
this->addItem(event->mimeData()->text());
event->setDropAction(Qt::MoveAction);
event->accept();
}
}
void ListWidget::performDrag()
{
QListWidgetItem *item = this->currentItem();
if (item) {
QMimeData *mimeData = new QMimeData;
mimeData->setText(item->text());
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
if (drag->exec(Qt::MoveAction) == Qt::MoveAction) {
delete item;
}
}
}