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);//从源路径将文件复制到目标路径
    }
}
自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取: https://www.cnblogs.com/bclshuai/p/11380657.html
原文地址:https://www.cnblogs.com/bclshuai/p/15725002.html