标签:
void QDragDrop::mousePressEvent(QMouseEvent *event) { __super::mousePressEvent(event); mStartPoint = event->pos();//起点 } void QDragDrop::mouseMoveEvent(QMouseEvent *event) { __super::mouseMoveEvent(event); if ((event->pos() - mStartPoint).manhattanLength() > QApplication::startDragDistance())//判断是否执行拖动 { QDrag *drag = new QDrag(this); QMimeData *mimeData = new QMimeData; QByteArray byteArray;//存储数据 QDataStream stream(&byteArray,QIODevice::WriteOnly);//流,写数据 stream << QImage("C:\\Users\\dlp\\Desktop\\2.png");//读取一个图片的数据 mimeData->setData("MyImage", byteArray);//设置数据 drag->setMimeData(mimeData);//设置数据 drag->exec(Qt::MoveAction);//执行拖动 delete drag; } }上述示例,读取了一个图片的数据保存在QMimeData里面,作为要传递的数据。并且设置了自定义格式"MyImage"。
#ifndef QDRAGDROP_H #define QDRAGDROP_H #include <QtWidgets/QWidget> #include <QMouseEvent> #include <QHBoxLayout> class QDragDrop : public QWidget { Q_OBJECT public: QDragDrop(QWidget *parent = 0); ~QDragDrop(); protected: void dragEnterEvent(QDragEnterEvent *event); void dragMoveEvent(QDragMoveEvent *event); void dropEvent(QDropEvent *event); void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); private: QPoint mStartPoint; QHBoxLayout *m_HBox; }; #endif // QDRAGDROP_H
#include "qdragdrop.h" #include <QApplication> #include <QDrag> #include <QMimeData> #include <QLabel> QDragDrop::QDragDrop(QWidget *parent) : QWidget(parent) { mStartPoint = QPoint(0, 0); m_HBox = new QHBoxLayout(this); setAcceptDrops(true); setWindowTitle("QWidget"); } QDragDrop::~QDragDrop() { } void QDragDrop::mousePressEvent(QMouseEvent *event) { __super::mousePressEvent(event); mStartPoint = event->pos();//起点 } void QDragDrop::mouseMoveEvent(QMouseEvent *event) { __super::mouseMoveEvent(event); if ((event->pos() - mStartPoint).manhattanLength() > QApplication::startDragDistance())//判断是否执行拖动 { QDrag *drag = new QDrag(this); QMimeData *mimeData = new QMimeData; QByteArray byteArray;//存储数据 QDataStream stream(&byteArray,QIODevice::WriteOnly);//流,写数据 stream << QImage("C:\\Users\\dlp\\Desktop\\2.png");//读取一个图片的数据 mimeData->setData("MyImage", byteArray);//设置数据 drag->setMimeData(mimeData);//设置数据 drag->exec(Qt::MoveAction);//执行拖动 delete drag; } } void QDragDrop::dragEnterEvent(QDragEnterEvent *event) { __super::dragEnterEvent(event); if (event->mimeData()->hasFormat("MyImage")) event->acceptProposedAction(); } void QDragDrop::dragMoveEvent(QDragMoveEvent *event) { __super::dragMoveEvent(event); if (event->mimeData()->hasFormat("MyImage")) event->acceptProposedAction(); } void QDragDrop::dropEvent(QDropEvent *event) { __super::dropEvent(event); if (event->mimeData()->hasFormat("MyImage")) { QByteArray byteArray = event->mimeData()->data("MyImage");//取数据 QDataStream stream(&byteArray, QIODevice::ReadOnly);//流,读数据 QImage image; stream >> image; QLabel *pLabel = new QLabel(this); pLabel->setPixmap(QPixmap::fromImage(image)); m_HBox->addWidget(pLabel); } }
#ifndef QSHOWIMAGE_H #define QSHOWIMAGE_H #include <QWidget> #include <QHBoxLayout> #include <QDragEnterEvent> #include <QMimeData> class QShowImage : public QWidget { Q_OBJECT public: QShowImage(QWidget *parent = 0); ~QShowImage(); protected: void dragEnterEvent(QDragEnterEvent *event); void dropEvent(QDropEvent *event); private: QHBoxLayout *mLayout; }; #endif // QSHOWIMAGE_H
#include "QShowImage.h" #include <QLabel> #include <QDebug> QShowImage::QShowImage(QWidget *parent) : QWidget(parent) { mLayout = new QHBoxLayout(this); setAcceptDrops(true); setWindowTitle("ShowImage"); } QShowImage::~QShowImage() { } void QShowImage::dragEnterEvent(QDragEnterEvent *event) { __super::dragEnterEvent(event); if (event->mimeData()->hasUrls()) event->acceptProposedAction(); } void QShowImage::dropEvent(QDropEvent *event) { __super::dropEvent(event); if (event->mimeData()->hasUrls()) { QList<QUrl> urls = event->mimeData()->urls(); for each (QUrl url in urls) { QLabel *pLabel = new QLabel(this); QImage image(url.toString().mid(8)); pLabel->setPixmap(QPixmap::fromImage(image)); mLayout->addWidget(pLabel); } } }
QListWidget qlistWidget; QStringList strList = { "Item1", "Item2", "Item3", "Item4", "Item5" }; qlistWidget.addItems(strList); qlistWidget.setViewMode(QListView::IconMode); qlistWidget.setDragEnabled(true); qlistWidget.show();但是上述只是移动了位置,Item的具体索引并没有发生改变。而且如果我们需要实现其他拖放功能,并不能满足我们的需求。那么我们就需要重新实现拖放事件,来自定义我们自己需要实现的功能。我实现了如何拖动Item,然后再添加一个Item的功能,两个QListWidgetItem并没有什么关联,只是为了展示如何实现。
#ifndef LISTWIDGET_H #define LISTWIDGET_H #include <QListWidget> #include <QMouseEvent> class ListWidget : public QListWidget { Q_OBJECT public: ListWidget(QWidget *parent = 0); ~ListWidget(); void dragEnterEvent(QDragEnterEvent *event); void dragMoveEvent(QDragMoveEvent *event); void dropEvent(QDropEvent *event); private: }; #endif // LISTWIDGET_H
#include "ListWidget.h" #include <QDrag> #include <QMimeData> #include <QLabel> ListWidget::ListWidget(QWidget *parent) : QListWidget(parent) { QStringList strList = { "Item1", "Item2", "Item3", "Item4", "Item5" }; addItems(strList); setViewMode(QListView::IconMode); setDragEnabled(true); setWindowTitle("QListWidget"); } ListWidget::~ListWidget() { } void ListWidget::dragEnterEvent(QDragEnterEvent *event) { __super::dragEnterEvent(event); ListWidget *pList = qobject_cast<ListWidget *>(event->source());//拖动的源对象 if (pList && pList == this) { event->setDropAction(Qt::DropAction::CopyAction);//设置拖动动作,影响鼠标光标 event->accept(); } } void ListWidget::dragMoveEvent(QDragMoveEvent *event) { __super::dragMoveEvent(event); ListWidget *pList = qobject_cast<ListWidget *>(event->source());//拖动的源对象 if (pList && pList == this) { event->setDropAction(Qt::DropAction::CopyAction);//设置拖动动作,影响鼠标光标 event->accept(); } } void ListWidget::dropEvent(QDropEvent *event) { __super::dropEvent(event); ListWidget *pList = qobject_cast<ListWidget *>(event->source());//拖动的源对象 if (pList && pList == this) { QListWidgetItem *pItem = new QListWidgetItem(this);//执行释放后,要做的事情 pItem->setText(QString("Item").append(QString::number(count()))); addItem(pItem); } }
标签:
原文地址:http://blog.csdn.net/u011417605/article/details/51316037