QT读取剪贴板信息和拖动文件到程序以(拖动音乐播放为列)

2 篇文章 0 订阅
1 篇文章 0 订阅


来句心灵毒药一个人越懒,明天要做的事情就越多


------------------------------------------------------------------------------------------------------华丽的分割线-------------------------------------------------------------------------------------------------


有时候看到很多程序可以直接拖一个文件程序就可以打开是不是很炫酷呀!

那么我们今天就干这件事

首先写出来我们需要重写的两个虚函数

protected:
	QMediaPlayer *mediaPlayer;
	void dragEnterEvent(QDragEnterEvent *event);//拖动事件
	void dropEvent(QDropEvent * event); //放下事件
	bool readFile(const QString &fileNmae);
	QLineEdit *lineEdit;
首先我们需要重写拖动事件,和放下事件,不信你想想是不是那个道理,先拖动,再放下。


我们在来看看拖动事件的具体实现:

void DragAndDrop::dragEnterEvent(QDragEnterEvent *event){
	if (event->mimeData()->hasFormat("text/uri-list"))//mimeData()是一个mime集合,mime集合具体可以百度,这里判断他支持的文件类型,为文本
	{
		event->acceptProposedAction();//如果是这个类型我们就调用accepProposedAction使他接受这个拖放
	}
}
这个,拖动事件写的很详细了,excuse me?


我们再来看看放下事件


void DragAndDrop::dropEvent(QDropEvent * event){
	QList<QUrl>urls = event->mimeData()->urls();//urls获取文件的集合,有时可能你会拖动选中的一片文件
	if (urls.isEmpty())
	{
		return;
	}
	QString fileName = urls.first().toLocalFile();//这里我们只取第一个,并准换为地址
	if (fileName.isEmpty())
		return;
	if (readFile(fileName))
	{
		//ok
		return;
	}
}//读取文件判断类型该干什么。
bool DragAndDrop::readFile(const QString &fileName){
	QStringList fileListItem = fileName.split(".");//得到文件的类型
	if (fileListItem.size()!=0)
	{<span style="white-space:pre">	</span>//如果是MP3或者wma就是播放音乐
		if (((fileListItem.last().toLower()) == "mp3") || ((fileListItem.last().toLower()) == "wma"))
		{
			mediaPlayer->stop();
			mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
			mediaPlayer->play();
			QMessageBox::information(this, QString::fromLocal8Bit("正在播放:"), fileName);
			return true;
		}
		else if ((fileListItem.last().toLower()) == "txt")
		{

		}
	}
	else{
		QMessageBox::information(this, QString::fromLocal8Bit("不能识别拖动的文件名"), fileName);
	}
	QMessageBox::information(this, QString::fromLocal8Bit("拖动的文件名"), fileName); 
	return true;
}
放下事件一切都是这么简单,


---------------------------------------------------------------------------------------------------------华丽的分割线------------------------------------------------------------------------------------------------------


以下是得到剪贴板的信息的代码:

QClipboard *clipboard = QApplication::clipboard();
connect(clipboard, SIGNAL(dataChanged()), this, SLOT(readClipboard()));//这里定义一个槽用于接收剪贴板的时时改变


readClipboard的具体实现:

void DragAndDrop::readClipboard(){
	QClipboard* clipboard = qobject_cast<QClipboard*>(QObject::sender());//获得发送信号的源对象 
	QString text = clipboard->text();//获得剪贴板的文本,图片需要用image(),
	lineEdit->setText(text);
}


来张程序运行截图:


---------------------------------------------------------------------------------------------------------华丽的分割线---------------------------------------------------------------------------------------------------------

然后博主还是把整个文件的代码放出来吧:



.h文件的代码:

#include <QWidget>
#include <QMainWindow>
#include <QDragEnterEvent>
#include <QLabel>
#include <QTextEdit>
#include <QSizePolicy>
#include <QMimeData>
#include <QClipboard>
#include <QApplication>
#include <QSize>
#include <QMediaPlayer>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QLineEdit>
class DragAndDrop:public QWidget
{
	Q_OBJECT
public:
	DragAndDrop(QWidget *parent=0);
	~DragAndDrop();
protected:
	QMediaPlayer *mediaPlayer;
	void dragEnterEvent(QDragEnterEvent *event);
	void dropEvent(QDropEvent * event); 
	bool readFile(const QString &fileNmae);
	QLineEdit *lineEdit;
private slots:
	void readClipboard();
};


.cpp的代码

#include "draganddrop.h"


DragAndDrop::DragAndDrop(QWidget *parent) :QWidget(parent)
{
	setAcceptDrops(true);
	
	QLabel *dragText = new QLabel(QString::fromLocal8Bit("请拖动到下面系统自动识别文件类型"
		"(歌曲可以播放):")); 
	QLabel *clipboardText = new QLabel(QString::fromLocal8Bit("剪贴板:"));
	lineEdit = new QLineEdit; 
	clipboardText->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

	QClipboard *clipboard = QApplication::clipboard();
	connect(clipboard, SIGNAL(dataChanged()), this, SLOT(readClipboard()));
 
	mediaPlayer = new QMediaPlayer;
	QVBoxLayout *qhb = new QVBoxLayout;
	qhb->setSpacing(0);
	qhb->addWidget(clipboardText);
	qhb->addWidget(lineEdit);
	qhb->addWidget(dragText);
	qhb->addStretch();  
	setLayout(qhb);

	this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	this->setMinimumHeight(500);
	this->setMinimumWidth(500);
}
void DragAndDrop::readClipboard(){
	QClipboard* clipboard = qobject_cast<QClipboard*>(QObject::sender());//获得发送信号的源对象 
	QString text = clipboard->text();
	lineEdit->setText(text);
}
void DragAndDrop::dragEnterEvent(QDragEnterEvent *event){
	if (event->mimeData()->hasFormat("text/uri-list"))
	{
		event->acceptProposedAction();
	}
}
void DragAndDrop::dropEvent(QDropEvent * event){
	QList<QUrl>urls = event->mimeData()->urls();
	if (urls.isEmpty())
	{
		return;
	}
	QString fileName = urls.first().toLocalFile();
	if (fileName.isEmpty())
		return;
	if (readFile(fileName))
	{
		//ok
		return;
	}
}
bool DragAndDrop::readFile(const QString &fileName){
	QStringList fileListItem = fileName.split(".");
	if (fileListItem.size()!=0)
	{
		if (((fileListItem.last().toLower()) == "mp3") || ((fileListItem.last().toLower()) == "wma"))
		{
			mediaPlayer->stop();
			mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
			mediaPlayer->play();
			QMessageBox::information(this, QString::fromLocal8Bit("正在播放:"), fileName);
			return true;
		}
		else if ((fileListItem.last().toLower()) == "txt")
		{

		}
	}
	else{
		QMessageBox::information(this, QString::fromLocal8Bit("不能识别拖动的文件名"), fileName);
	}
	QMessageBox::information(this, QString::fromLocal8Bit("拖动的文件名"), fileName); 
	return true;
}
DragAndDrop::~DragAndDrop()
{
}


----------------------------------------------------------------------------------------------------------- 华丽的结束线-------------------------------------------------------------------------------------------------------

------------------

-----------

------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一路随云00000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值