码迷,mamicode.com
首页 > 其他好文 > 详细

关于QListWidget的Drag和Drop用法

时间:2014-05-06 23:19:08      阅读:403      评论:0      收藏:0      [点我收藏+]

标签: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);
}

简单方法, 其实只需要设置这几个属性就OK了..:

上图:

bubuko.com,布布扣

bubuko.com,布布扣

唉.. 不作死就不会死....

关于QListWidget的Drag和Drop用法,布布扣,bubuko.com

关于QListWidget的Drag和Drop用法

标签:drag   drop   qt   qlistwidget   

原文地址:http://blog.csdn.net/robertkun/article/details/25127921

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!