QT读取剪贴板信息和拖动文件到程序以(拖动音乐播放为列)
2016-10-20 11:26
501 查看
来句心灵毒药:一个人越懒,明天要做的事情就越多
------------------------------------------------------------------------------------------------------华丽的分割线-------------------------------------------------------------------------------------------------
有时候看到很多程序可以直接拖一个文件到程序就可以打开是不是很炫酷呀!
那么我们今天就干这件事
首先写出来我们需要重写的两个虚函数:
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()
{
}
-----------------------------------------------------------------------------------------------------------华丽的结束线-------------------------------------------------------------------------------------------------------
------------------
-----------
------
------------------------------------------------------------------------------------------------------华丽的分割线-------------------------------------------------------------------------------------------------
有时候看到很多程序可以直接拖一个文件到程序就可以打开是不是很炫酷呀!
那么我们今天就干这件事
首先写出来我们需要重写的两个虚函数:
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()
{
}
-----------------------------------------------------------------------------------------------------------华丽的结束线-------------------------------------------------------------------------------------------------------
------------------
-----------
------
相关文章推荐
- vc2008: 控制台程序将数据写入MySql(数据库信息从配置文件读取)
- 从"QQ同步助手"上导出的短信文件*.csv中读取短信,并运用在Qt程序中
- 用Qt读取xml文件,程序执行到 if(!doc.setContent(&file)) 时候出错
- QT实现图库程序(一)读取指定目录下的所有文件
- 税务催缴催报信息打印程序开发-Delphi读取Excel文件及操作
- Qt 开启自启动程序 启动后仍可读取本地文件
- <QT实现音乐播放器2> 添加音乐播放进度条与歌名作者信息
- 微信小程序-图片、录音、音频播放、音乐播放、视屏、文件
- 读取剪贴板文件信息
- 002_010 Python 处理字符串中的Zip文件 你的程序收到了一个字符串,内容是一个ZIP文件,需要读取zip文件的信息
- C#读取歌曲列表获取单个音乐文件信息
- AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件
- 【QT5.5开发】QMediaPlayerl类实现mp3音乐播放的控制台程序
- C下学生管理系统:从文件中读取30位学生的信息(含邮箱),并实现简单的增、删、查找、统计(邮箱使用人数)。---附程序哦!
- 用java程序读取xml文件中的配置信息
- python下学生管理系统:从文件中读取30位学生的信息(含邮箱),并实现简单的增、删、查找、统计(邮箱使用人数)。---附程序哦!
- Qt 使用 phonon 播放 wav音乐文件 的 demo
- SSM框架day02-MyBatis——011第一个程序-显示日志信息、012-定义和使用工具类、013从属性文件读取DB连接四要素
- IOS学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件
- [QT笔记]在Qt程序中使用Windows API读取ini设置信息