1、概述
源码放在文章末尾
该项目基于QT+QML实现了文件传输的功能,可以在局域网环境下使用(热点),扫描使用UDP,传输使用TCP,每一个文件传输使用独立的线程进行处理,高效便捷。
开发环境
使用Qt/Qml开发
QT版本:Qt 5.13.0
基本未使用高版本特性,保证Qt Version >= 5.0即可,建议是Qt 5.5 ~ 5.7
项目demo演示如下:
项目部分代码如下所示:
#ifndef FILEMANAGER_H
#define FILEMANAGER_H
#include <QObject>
#include <QQmlListProperty>
class FileInfo : public QObject
{
Q_OBJECT
Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged)
Q_PROPERTY(int offset READ offset WRITE setOffset NOTIFY offsetChanged)
Q_PROPERTY(int fileSize READ fileSize WRITE setFileSize NOTIFY fileSizeChanged)
public:
FileInfo(QObject *parent = nullptr);
FileInfo(const FileInfo &other, QObject *parent = nullptr);
FileInfo(const QString &fileName, int offset, int fileSize, QObject *parent = nullptr);
~FileInfo();
QString fileName() const;
void setFileName(const QString &fileName);
int offset() const;
void setOffset(int offset);
int fileSize() const;
void setFileSize(int fileSize);
signals:
void fileNameChanged();
void offsetChanged();
void fileSizeChanged();
private:
QString m_fileName;
int m_offset;
int m_fileSize;
};
class FileManager : public QObject
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<FileInfo> readFiles READ readFiles NOTIFY readFilesChanged)
Q_PROPERTY(QQmlListProperty<FileInfo> writeFiles READ writeFiles NOTIFY writeFilesChanged)
public:
static FileManager* instance();
~FileManager();
QQmlListProperty<FileInfo> readFiles();
QQmlListProperty<FileInfo> writeFiles();
public slots:
void addReadFile(const QString &fileName, qint32 totalSize);
void updateReadFile(const QString &fileName, qint32 offset);
void addWriteFile(const QString &fileName, qint32 totalSize);
void updateWriteFile(const QString &fileName, qint32 offset);
signals:
void readFilesChanged();
void writeFilesChanged();
void readFileComplete(const QString &fileName);
void writeFileComplete(const QString &fileName);
private:
FileManager(QObject *parent = nullptr);
QHash<QString, FileInfo*> m_filesTable;
QList<FileInfo *> m_readFiles;
QQmlListProperty<FileInfo> *m_readFilesProxy;
QList<FileInfo *> m_writeFiles;
QQmlListProperty<FileInfo> *m_writeFilesProxy;
};
#endif // FILEMANAGER_H
#include "filemanager.h"
#include <QDebug>
FileInfo::FileInfo(QObject *parent)
: QObject (parent)
{
}
FileInfo::FileInfo(const FileInfo &other, QObject *parent)
: QObject (parent),
m_fileName(other.m_fileName),
m_offset(other.m_offset),
m_fileSize(other.m_fileSize)
{
}
FileInfo::FileInfo(const QString &fileName, int offset, int fileSize, QObject *parent)
: QObject (parent),
m_fileName(fileName),
m_offset(offset),
m_fileSize(fileSize)
{
}
FileInfo::~FileInfo()
{
}
QString FileInfo::fileName() const
{
return m_fileName;
}
void FileInfo::setFileName(const QString &fileName)
{
if (fileName != m_fileName) {
m_fileName = fileName;
emit fileNameChanged();
}
}
int FileInfo::offset() const
{
return m_offset;
}
void FileInfo::setOffset(int offset)
{
if (offset != m_offset) {
m_offset = offset;
emit offsetChanged();
}
}
int FileInfo::fileSize() const
{
return m_fileSize;
}
void FileInfo::setFileSize(int fileSize)
{
if (fileSize != m_fileSize) {
m_fileSize = fileSize;
emit fileSizeChanged();
}
}
FileManager* FileManager::instance()
{
static FileManager fileManager;
return &fileManager;
}
FileManager::FileManager(QObject *parent)
: QObject (parent)
{
m_readFilesProxy = new QQmlListProperty<FileInfo>(this, m_readFiles);
m_writeFilesProxy = new QQmlListProperty<FileInfo>(this, m_writeFiles);
}
FileManager::~FileManager()
{
}
QQmlListProperty<FileInfo> FileManager::readFiles()
{
return *m_readFilesProxy;
}
QQmlListProperty<FileInfo> FileManager::writeFiles()
{
return *m_writeFilesProxy;
}
void FileManager::addReadFile(const QString &fileName, qint32 totalSize)
{
FileInfo *info = new FileInfo(fileName, 0, totalSize, this);
m_readFiles.append(info);
m_filesTable[fileName] = info;
emit readFilesChanged();
}
void FileManager::updateReadFile(const QString &fileName, qint32 offset)
{
FileInfo *info = m_filesTable[fileName];
if (info) {
info->setOffset(offset);
if (offset == info->fileSize()) {
m_filesTable.remove(fileName);
emit writeFileComplete(fileName);
}
}
}
void FileManager::addWriteFile(const QString &fileName, qint32 totalSize)
{
FileInfo *info = new FileInfo(fileName, 0, totalSize, this);
m_writeFiles.append(info);
m_filesTable[fileName] = info;
emit writeFilesChanged();
}
void FileManager::updateWriteFile(const QString &fileName, qint32 offset)
{
FileInfo *info = m_filesTable[fileName];
if (info) {
info->setOffset(offset);
if (offset == info->fileSize()) {
m_filesTable.remove(fileName);
emit writeFileComplete(fileName);
}
}
}