Qt QListWidget拖拽实现

接着前面的QTableWidget继续写吧,原本实现QTableWidget的拖拽,结果还是没弄好,唉……一个字,搓!两个字,很搓!

QTableWidget拖拽没有实现,但是还是有希望的,希望至少还不是在田野上,QListWidget的拖拽实现了,亲们……

直接贴代码吧!

==========================================================================================

下面的实现是在一个控件里面拖拽Item,插入到鼠标释放的位置

#ifndef DRAG_TABLE_H
#define DRAG_TABLE_H

#include <QTableWidget>
#include <QListWidget>
#include <QtGui>
#include <QDrag>

class DragListWidget : public QListWidget
{
public:
	DragListWidget(QWidget *parent = 0);
protected:
	void mousePressEvent(QMouseEvent *event);
	void mouseMoveEvent(QMouseEvent *event);
	void dragEnterEvent(QDragEnterEvent *event);
	void dragMoveEvent(QDragMoveEvent *event);
	void dropEvent(QDropEvent *event);
public:
	QPoint startPos;
	QPoint endPos;
};

#endif
DragListWidget.cpp


#include "DragTable.h"

DragListWidget::DragListWidget(QWidget *parent):QListWidget(parent)
{
	this->setCurrentRow(10);
	addItem("1");
	addItem("2");
	addItem("3");
	addItem("4");
	addItem("5");
	addItem("6");
	addItem("7");
	addItem("8");
	addItem("9");
	addItem("10");
	this->setAcceptDrops(true);//设置QListWidget的Item项可以被拖拽
}

void DragListWidget::mousePressEvent( QMouseEvent *event )
{
	if (event->button() == Qt::LeftButton)
		startPos = event->pos();
	QListWidget::mousePressEvent(event);
}

void DragListWidget::mouseMoveEvent(QMouseEvent *event)
{
	QListWidgetItem *item = currentItem();
	mimeData = new QMimeData;
	mimeData->setText(item->text());
	QDrag *drag = new QDrag(this);
	drag->setMimeData(mimeData);
	//drag->setPixmap(pixmap);	
	Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
	if(dropAction == Qt::MoveAction)
	{
		delete item;//删除原有的item,在dropEvent(QDropEvent *event)函数中插入item
	}
}

void DragListWidget::mouseReleaseEvent(QMouseEvent *event)
{
	int distance = (endPos - startPos).manhattanLength();
	if (distance >= QApplication::startDragDistance())
	{
		startDrag();
	}
}

void DragListWidget::dragEnterEvent( QDragEnterEvent *event )
{
	DragListWidget *source = (DragListWidget *)((void*)(event->source()));
	if (source && source == this) {
		event->setDropAction(Qt::MoveAction);
		event->accept();
	}
}

void DragListWidget::dragMoveEvent( QDragMoveEvent *event )
{
	DragListWidget *source = (DragListWidget *)((void*)(event->source()));
	if (source && source == this) {
		event->setDropAction(Qt::MoveAction);
		event->accept();
	}
}

void DragListWidget::dropEvent( QDropEvent *event )
{
	DragListWidget *source = (DragListWidget *)((void*)(event->source()));
	if (source && source == this) 
	{
		endPos = event->pos();//得到鼠标移动到的坐标
		QListWidgetItem *itemRow = itemAt(endPos);.//通过endPos获取鼠标位置所在的行
		int rowCount = row(itemRow);
		insertItem(rowCount + 1,mimeData->text());//row+1 为鼠标所指向的行,如果只是向末尾位置插入,把insertItem(rowCount + 1,mimeData->text())改为addItem(mimeData->text())
		event->setDropAction(Qt::MoveAction);
		event->accept();
	}
}



效果如图:


===================================================================================

从其他的QListWidget中拖拽到另一个QListWidget

DragListWidget.h


#ifndef DRAG_TABLE_H
#define DRAG_TABLE_H

#include <QTableWidget>
#include <QListWidget>
#include <QtGui>
#include <QDrag>

class DragListWidget: public QListWidget
{
public:
	DragListWidget(QWidget *parent = 0);
protected:
	void mousePressEvent(QMouseEvent *event);
	void mouseMoveEvent(QMouseEvent *event);
	void mouseReleaseEvent(QMouseEvent *event);
	void dragEnterEvent(QDragEnterEvent *event);
	void dragMoveEvent(QDragMoveEvent *event);
	void dropEvent(QDropEvent *event);
public:
	QPoint startPos;
	QPoint endPos;
};

#endif

DragListWidget.cpp

#include "DragTable.h"

DragListWidget::DragListWidget(QWidget *parent):QListWidget(parent)
{
	this->setCurrentRow(10);
	addItem("1");
	addItem("2");
	addItem("3");
	addItem("4");
	addItem("5");
	addItem("6");
	addItem("7");
	addItem("8");
	addItem("9");
	addItem("10");
	this->setAcceptDrops(true);//设置QListWidget的Item项可以被拖拽
}

void DragListWidget::mousePressEvent( QMouseEvent *event )
{
	if (event->button() == Qt::LeftButton)
		startPos = event->pos();
	QListWidget::mousePressEvent(event);
}

void DragListWidget::mouseMoveEvent(QMouseEvent *event)
{
	if (event->buttons() & Qt::LeftButton) {
        int distance = (event->pos() - startPos).manhattanLength();
        if (distance >= QApplication::startDragDistance())
            startDrag();
    }
    QListWidget::mouseMoveEvent(event);
}

void DragListWidget::dragEnterEvent( QDragEnterEvent *event )
{
	DragListWidget *source = (DragListWidget *)((void*)(event->source()));
	if (source && source != this) {
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }
}

void DragListWidget::dragMoveEvent( QDragMoveEvent *event )
{
	DragListWidget *source = (DragListWidget *)((void*)(event->source()));
	if (source && source != this) {
		event->setDropAction(Qt::MoveAction);
		event->accept();
	}
}

void DragListWidget::dropEvent( QDropEvent *event )
{
	DragListWidget *source = (DragListWidget *)((void*)(event->source()));
	if (source && source != this) 
	{
		endPos = event->pos();
		QListWidgetItem *itemRow = itemAt(endPos);
		int rowCount = row(itemRow);
		insertItem(rowCount + 1,mimeData->text());
		//addItem(event->mimeData()->text());
		event->setDropAction(Qt::MoveAction);
		event->accept();
	}
}


效果如图:



有什么地方不对,还请大牛指点,学习中……



发布了8 篇原创文章 · 获赞 2 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览