Qt5 實現檔案複製並在指定路徑下建立資料夾
最近在基於qt5平臺的應用介面上想實現本地選擇檔案,並把檔案拷貝到指定路徑下。找到以下這篇文章參考,參考其中幾個函式的寫法實現了想要的功能,故記錄一下,方便以後查詢參考。
我在標頭檔案中定義幾個變數:
QFileDialog *mFileDlg;
QString mFileName;
QLineEdit *mFilePath;
在.cpp實現檔案中程式碼如下:
mFilePath = new QLineEdit(this);
mFileName = QFileDialog::getOpenFileName(this,tr("Open File"));
mFilePath->setText(mFileName);
HelperPrintf("current file name is: %s",mFileName);
QFile file(mFileName);
QFileInfo info(mFileName);
QString dstPath = QString(DST_FILE_PATH) + QString("/");
QDir directory ;
directory.cd(dstPath);
dstPath += info.fileName();
file.copy(dstPath);
file.close();
QFile checkPath(dstPath);
if(checkPath.open(QFile::ReadOnly))
{
QMessageBox msgBox;
msgBox.setText("File copy success!");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
}
checkPath.close();
部落格原文如下:
最近想做一個區域網的圖書管理的一個應用,希望能夠管理本地的電子書資源,同時分享給在同一個區域網的使用者們。因此在本地需要建立一個圖書的管理目錄出來(暫時是這樣想的),因此需要對電腦上的資源進行統一的移動和複製,同時將可能以後需要的資訊進行儲存下來,在Qt中可能設計到相關內容的包含:
- QFile 的使用
- QDir 的使用
- QFileInfo的使用
- QFileDialog的使用
QDir類
QFileInfo類可以提供與作業系統無關的檔案資訊,包含檔案的名稱和檔案系統的位置,檔案的訪問許可權以及是否是一個目錄。可以通過函式path()和fileName()獲取檔案的路徑和檔名,同時baseName()可以獲得檔案的基本名稱,即不包含字尾名。
下面一個簡單的例項,可以通過選擇需要複製的檔案,複製到相應的目錄下。同時根據檔名,在相應的目錄下建立對應檔案的子目錄。實現的是,在一個介面進行檔案的操作,包括錄入圖書的資訊,包括書名,作者以及出版商,以及書的封面,然後儲存。
標頭檔案內容為:
- #ifndef UPLOADFRAME_H
- #define UPLOADFRAME_H
- #include <QWidget>
- #include<QFrame>
- class QLabel;
- class QIcon;
- class QPushButton;
- class QLineEdit;
- class QTextBrower;
- class QToolButton;
- class UpLoadFrame : public QWidget
- {
- Q_OBJECT
- public:
- explicit UpLoadFrame(QWidget *parent = 0);
- void init();
- QString getWritername();
- QString getBookname();
- QString getPublisername();
- QString getAboutBook();
- QPixmap getBookfage();
- signals:
- protected slots:
- void doChoosepathbutton();
- void doSaveButton();
- void doBookFaceClicked();
- private:
- QLineEdit *filepath;
- QPushButton *choosepath;
- QToolButton *bookface;
- QLineEdit *writername;
- QLineEdit *publisher;
- QLineEdit *bookname;
- QLineEdit *aboutbook;
- QPushButton *saveButton;
- QString fileName;
- QString picpath;
- };
- #endif // UPLOADFRAME_H
- void UpLoadFrame::doChoosepathbutton()
- {
- fileName =QFileDialog::getOpenFileName(this,tr("開啟檔案"));
- filepath->setText(fileName);
- QFileInfo info(fileName);
- bookname->setText(info.baseName());
- }
QFileDialog可以建立一個對話方塊,允許用於選擇檔案和目錄。getOpenFileName()函式可以選擇一個已經存在於目錄下的檔案,如果取消了檔案的選擇,就會返回已給空的指標。第一個引數代表父空間的指標地址,第二個是代表對話方塊的名稱。
其次是實現doSaveButton(),當得到需要複製的檔案後,就需要比較負責的檔案操作了,比如檔案的複製,目錄的建立以及文字檔案的資訊的寫入。以下是實現的內容:
- void UpLoadFrame::doSaveButton()
- {
- QFile file(fileName);
- QFileInfo info(fileName);
- QString newpath=QString("G:/Reader_Resource")+QString("/");
- //------------------------建立對應書籍的目錄------------------------//
- QDir dir;
- dir.cd(newpath);
- dir.mkdir(info.baseName());
- newpath+=info.baseName();
- //----------------讀取圖書的資訊並儲存在對應的目錄下-----------------//
- QFile bookinfo(newpath+"/"+info.baseName()+".txt");
- QFileInfo tmp(bookinfo);
- aboutbook->setText(tmp.absoluteFilePath());
- if( bookinfo.open(QFile::ReadWrite))
- {
- QTextStream out(&bookinfo);
- out<<writername->text()<<"\r\n"<<endl;
- out<<bookname->text()<<"\r\n"<<endl;
- out<<publisher->text()<<"\r\n"<<endl;
- }
- bookinfo.setFileName(info.baseName()+".txt");
- bookinfo.close();
- //--------------------------------------------------------------//
- newpath+=QString("/");
- //----