1. 程式人生 > 其它 >Qt檔案選擇複製另存為

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