自己開發了一個股票軟件,功能很強大,需要的點擊下面的鏈接獲取:
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);//從源路徑將文件複製到目標路徑 } }