标签:drag drop qt qlistwidget
QListWIdget的Drag和Drop. 真是坑爹到不行...
其实很简单的.. 最后搞到好多行代码..
真是作死的节奏...............
先上代码:<这种方法也可以实现, 但是太麻烦了...>
#include "PlayerDragWidget.h" #include "PlayerLyricsListItem.h" #include <QDrag> #include <QPainter> #include <QDragEnterEvent> #include <QList> #include <QDebug> PlayerDragWidget::PlayerDragWidget(QWidget *parent) : QListWidget(parent) { setAcceptDrops(true); } PlayerDragWidget::~PlayerDragWidget() { } void PlayerDragWidget::dragEnterEvent( QDragEnterEvent * event ) { PlayerDragWidget *source = (PlayerDragWidget *)((void*)(event->source())); if (source && source == this) { event->setDropAction(Qt::MoveAction); event->accept(); } else { event->setDropAction(Qt::CopyAction); event->accept(); } } void PlayerDragWidget::dragMoveEvent( QDragMoveEvent * event ) { PlayerDragWidget *source = (PlayerDragWidget *)((void*)(event->source())); if (source && source == this) { event->setDropAction(Qt::MoveAction); event->accept(); } } void PlayerDragWidget::dragLeaveEvent( QDragLeaveEvent * event ) { //PlayerDragWidget *source = (PlayerDragWidget *)((void*)(event->source())); //if (source && source == this) { // event->setDropAction(Qt::MoveAction); // event->accept(); //} //else //{ // event->dra //} } void PlayerDragWidget::dropEvent( QDropEvent * event ) { PlayerDragWidget *source = (PlayerDragWidget *)((void*)(event->source())); if (source && source == this) { endPos = event->pos();//得到鼠标移动到的坐标 QListWidgetItem *itemRow = itemAt(endPos); if(itemRow) { const QMimeData* data = event->mimeData(); QVariant var; if(data->hasColor()) { var = data->colorData(); } MusicInfo songInfo; if(var.canConvert<MusicInfo>()) { songInfo = var.value<MusicInfo>(); } int rowInsert = row(itemRow); emit sigItemDragDrop(rowInsert+1, songInfo); } else { const QMimeData* data = event->mimeData(); QVariant var; if(data->hasColor()) { var = data->colorData(); } MusicInfo songInfo; if(var.canConvert<MusicInfo>()) { songInfo = var.value<MusicInfo>(); } emit sigItemDragDrop(count(), songInfo); } event->setDropAction(Qt::MoveAction); event->accept(); } else { QList<QUrl> urls = event->mimeData()->urls(); if(urls.isEmpty()) return; emit sigAddDragItems(urls); event->accept(); } } void PlayerDragWidget::mousePressEvent( QMouseEvent *event ) { if (event->button() == Qt::LeftButton) startPos = event->pos(); QListWidget::mousePressEvent(event); } void PlayerDragWidget::mouseMoveEvent(QMouseEvent *event) { QListWidgetItem *item = currentItem(); mimeData = new QMimeData; MusicInfo info; QWidget* wid = itemWidget(item); if(wid) { PlayerLyricsListItem* lyricsWid = qobject_cast<PlayerLyricsListItem*>(wid); if(lyricsWid) { info = lyricsWid->GetItemInfo(); QVariant var = QVariant::fromValue(info); mimeData->setColorData(var); } } QDrag *drag = new QDrag(this); drag->setMimeData(mimeData); QPixmap pixmap(QSize(175,20)); QPainter p(&pixmap); p.drawText(QRectF(10, 0, 175, 20), Qt::AlignVCenter, info.strTitle+" - "+info.strArtist); drag->setPixmap(pixmap); Qt::DropAction dropAction = drag->exec(Qt::MoveAction, Qt::MoveAction); if(dropAction == Qt::MoveAction) { delete item;//删除原有的item,在dropEvent(QDropEvent *event)函数中插入item } QListWidget::mouseMoveEvent(event); }
上图:
唉.. 不作死就不会死....
关于QListWidget的Drag和Drop用法,布布扣,bubuko.com
标签:drag drop qt qlistwidget
原文地址:http://blog.csdn.net/robertkun/article/details/25127921