Qt文件选择复制另存为
自己开发了一个股票软件,功能很强大,需要的点击下面的链接获取:
https://www.cnblogs.com/bclshuai/p/11380657.html
QT文件选择复制和另存为
目录
1 选择文件... 1
2 另存为... 2
1 选择文件
从电脑选择文件到程序中进行处理,可以设置文件的路径
函数名称
static QStringList getOpenFileNames(QWidget *parent = Q_NULLPTR,
const QString &caption = QString(),//窗口名称
const QString &dir = QString(),//文件夹路径
const QString &filter = QString(),//选择类型过滤器
QString *selectedFilter = Q_NULLPTR,
Options options = Options());
示例
QStringList fileNameList = QFileDialog::getOpenFileNames(this, tr("添加图片"), m_strDefaultPicPath, tr("Images(*.png *.jpeg *.jpg *.bmp *.tif *.tiff *.PNG *.JPEG *.JPG *.BMP *.TIF *.TIFF)"));
2 另存为
将程序中展示的本地图片另存为到另外一个路径下。应用场景,例如我将视频中的人脸截图都显示在程序界面,需要选择其中嫌疑犯的图片然后保存到另外一个文件夹作为档案存储起来。如下图所示
实现实例
(1) 选择要保存的路径
//选择保存的文件路径
QFileDialog fileDialog;
QString strTargetFile = fileDialog.getExistingDirectory(this, tr("选择保存路径"), m_strDefaultPicPath);
QDir dir(strTargetFile);
if (!dir.exists())
{
SlotError(-1, "请选择需要保存文件夹路径");
return-1;
}
(2)用一个Qt线程类实现文件的复制
因为文件数量比较大时,复制文件比较耗时,用主线程复制文件会造成界面卡顿,所以采用线程的方式复制文件。
#ifndef COPYFILETHREAD_H #define COPYFILETHREAD_H #include <QThread> class CopyFileThread : public QThread { Q_OBJECT public: CopyFileThread(); ~CopyFileThread(); int StartCopyFile(QStringList& listSourcefile,QString strTargetPath); void run(); private: QStringList m_listSourcefile; QString m_strTargetPath = ""; }; #endif // COPYFILETHREAD_H
源文件
#include "CopyFileThread.h" #include<QFileInfo> CopyFileThread::CopyFileThread() { } CopyFileThread::~CopyFileThread() { } int CopyFileThread::StartCopyFile(QStringList & listSourcefile, QString strTargetPath) { m_listSourcefile = listSourcefile; m_strTargetPath = strTargetPath; this->start(); return 0; } void CopyFileThread::run() { QString strSourcePath = ""; QString strTargetPath = ""; for (int i=0;i<m_listSourcefile.size();i++) { strSourcePath = m_listSourcefile[i]; QFileInfo file(strSourcePath); if (!file.exists()) { continue; } strTargetPath = m_strTargetPath + "/" + file.fileName(); QFile::copy(strSourcePath, strTargetPath);//从源路径将文件复制到目标路径 } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2019-12-24 股票主动资金变化曲线分析交易意向和趋势