Qt5 实现文件复制并在指定路径下创建文件夹

来源:互联网 发布:网络的特点是什么 编辑:程序博客网 时间:2024/06/01 12:15

最近在基于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的使用
QFile类提供了读取文件和操作文件的接口,可以读写文本文件以及二进制文件,以及资源文件。通常常用的函数包含open()close()函数的使用。

QDir类使得可以访问目录结构以及目录下的文件信息。可以操作路径名、访问路径以及文件的相关信息。可以通过mkdir()创建目录以及重命名目录。

QFileInfo类可以提供与操作系统无关的文件信息,包含文件的名称和文件系统的位置,文件的访问权限以及是否是一个目录。可以通过函数path()fileName()获取文件的路径和文件名,同时baseName()可以获得文件的基本名称,即不包含后缀名。

下面一个简单的实例,可以通过选择需要复制的文件,复制到相应的目录下。同时根据文件名,在相应的目录下创建对应文件的子目录。实现的是,在一个界面进行文件的操作,包括录入图书的信息,包括书名,作者以及出版商,以及书的封面,然后保存。

头文件内容为:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #ifndef UPLOADFRAME_H  
  2. #define UPLOADFRAME_H  
  3.   
  4. #include <QWidget>  
  5. #include<QFrame>  
  6. class QLabel;  
  7. class QIcon;  
  8. class QPushButton;  
  9. class QLineEdit;  
  10. class QTextBrower;  
  11. class QToolButton;  
  12. class UpLoadFrame : public QWidget  
  13. {  
  14.     Q_OBJECT  
  15. public:  
  16.     explicit UpLoadFrame(QWidget *parent = 0);  
  17.     void init();  
  18.     QString getWritername();  
  19.     QString getBookname();  
  20.     QString getPublisername();  
  21.     QString getAboutBook();  
  22.     QPixmap getBookfage();  
  23. signals:  
  24.   
  25. protected slots:  
  26.     void doChoosepathbutton();  
  27.     void doSaveButton();  
  28.     void doBookFaceClicked();  
  29.   
  30. private:  
  31.     QLineEdit *filepath;  
  32.     QPushButton *choosepath;  
  33.     QToolButton *bookface;  
  34.     QLineEdit *writername;  
  35.     QLineEdit *publisher;  
  36.     QLineEdit *bookname;  
  37.     QLineEdit *aboutbook;  
  38.     QPushButton *saveButton;  
  39.     QString fileName;  
  40.     QString picpath;  
  41. };  
  42.   
  43. #endif // UPLOADFRAME_H  
重点是实现三个槽。首先是第一个槽,doChoosepathbutton(),通过店家choosepath按键,打开一个选择文件的对话框选择文件,在该函数中实现文件路径的读取,通过文件的读取,自动设置书名。实现内容如下:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. void UpLoadFrame::doChoosepathbutton()  
  2. {  
  3.     fileName =QFileDialog::getOpenFileName(this,tr("打开文件"));  
  4.     filepath->setText(fileName);  
  5.     QFileInfo info(fileName);  
  6.     bookname->setText(info.baseName());  
  7. }  
其中一个关键的只是点是关于QFileDialog的知识点:

QFileDialog可以创建一个对话框,允许用于选择文件和目录。getOpenFileName()函数可以选择一个已经存在于目录下的文件,如果取消了文件的选择,就会返回已给空的指针。第一个参数代表父空间的指针地址,第二个是代表对话框的名称。

其次是实现doSaveButton(),当得到需要复制的文件后,就需要比较负责的文件操作了,比如文件的复制,目录的创建以及文本文件的信息的写入。以下是实现的内容:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. void UpLoadFrame::doSaveButton()  
  2. {  
  3.     QFile file(fileName);  
  4.     QFileInfo info(fileName);  
  5.     QString newpath=QString("G:/Reader_Resource")+QString("/");  
  6.   
  7.    //------------------------创建对应书籍的目录------------------------//  
  8.     QDir dir;  
  9.     dir.cd(newpath);  
  10.     dir.mkdir(info.baseName());  
  11.     newpath+=info.baseName();  
  12.     //----------------读取图书的信息并保存在对应的目录下-----------------//  
  13.   
  14.   
  15.     QFile bookinfo(newpath+"/"+info.baseName()+".txt");  
  16.     QFileInfo tmp(bookinfo);  
  17.     aboutbook->setText(tmp.absoluteFilePath());  
  18.     if( bookinfo.open(QFile::ReadWrite))  
  19.     {  
  20.   
  21.         QTextStream out(&bookinfo);  
  22.         out<<writername->text()<<"\r\n"<<endl;  
  23.         out<<bookname->text()<<"\r\n"<<endl;  
  24.         out<<publisher->text()<<"\r\n"<<endl;  
  25.     }  
  26.   
  27.     bookinfo.setFileName(info.baseName()+".txt");  
  28.     bookinfo.close();  
  29.     //--------------------------------------------------------------//  
  30.   
  31.   
  32.     newpath+=QString("/");  
  33.   
  34.     //----------------------复制图片封面信息------------------------//  
  35.     QFile picfile(picpath);  
  36.     QFileInfo picinfo(picfile);  
  37.     QString temptopicpath=newpath;  
  38.     temptopicpath+=picinfo.fileName();  
  39.     picfile.copy(temptopicpath);  
  40.     picfile.close();  
  41.   
  42.     //-------------------------复制文件到相应目录------------------------//  
  43.     newpath+=info.fileName();  
  44.     file.copy(newpath);  
  45.     file.close();  
  46.   
  47.   
  48.   
  49.     QFile checkpath(newpath);  
  50.     if(checkpath.open(QFile::ReadOnly))  
  51.     {  
  52.         QMessageBox msgBox;  
  53.         msgBox.setText("文件复制成功");  
  54.         msgBox.setStandardButtons(QMessageBox::Ok);  
  55.         msgBox.exec();  
  56.     }  
  57.     checkpath.close();  
  58.   
  59. }  
对以上的代码实现进行简答的解释:

1、对doChoopathbutton()函数中得到的fileName进行文件的读取,即QFile(fileName);

2、对fileName进行文件信息的解析,即通过QFileInfo实现;

3、在对应的newpath下目录下创建目录,目录名为需要复制的文件的文件名,不包含后缀名,这里文件名默认为书名

4、bookinfo是对书名、作者以及出版商信息的读取,并且写入到文本文件中;

5、copy函数是实现文件的复制的关键函数,QFile类的成员函数copy()的参数代表需要复制的新的文件的绝对路径信息。

最后实现的结果如图所示:

最后实现的结果如图所示:



可以看到,最后实现了需要达到的目。

完成的实现代码如下:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "uploadframe.h"  
  2. #include<QLabel>  
  3. #include<QIcon>  
  4. #include<QLineEdit>  
  5. #include<QPushButton>  
  6. #include<QTextBrowser>  
  7. #include<QToolButton>  
  8. #include<QHBoxLayout>  
  9. #include<QVBoxLayout>  
  10. #include<QFile>  
  11. #include<QFIleDialog>  
  12. #include<QFileInfo>  
  13. #include<QMessageBox>  
  14. #include<QDir>  
  15. #include<QTextStream>  
  16. UpLoadFrame::UpLoadFrame(QWidget *parent) : QWidget(parent)  
  17. {  
  18.   
  19.     filepath=new QLineEdit(this);  
  20.     choosepath=new QPushButton(this);  
  21.     bookface=new QToolButton();  
  22.     writername=new QLineEdit(this);  
  23.     publisher=new QLineEdit(this);  
  24.     bookname=new QLineEdit(this);  
  25.     aboutbook=new QLineEdit(this);  
  26.     init();  
  27.     connect(choosepath,SIGNAL(clicked(bool)),this,SLOT(doChoosepathbutton()));  
  28.     connect(saveButton,SIGNAL(clicked(bool)),this,SLOT(doSaveButton()));  
  29.     connect(bookface,SIGNAL(clicked(bool)),this,SLOT(doBookFaceClicked()));  
  30.     setFixedSize(400,300);  
  31. }  
  32.   
  33. void UpLoadFrame::init()  
  34. {  
  35.     QVBoxLayout *mainLayout=new QVBoxLayout(this);  
  36.     QHBoxLayout *hLayout_one=new QHBoxLayout(this);  
  37.   
  38.     filepath->setMinimumSize(250,20);  
  39.     filepath->setMaximumSize(250,20);  
  40.     filepath->setContentsMargins(0,0,0,0);  
  41.     choosepath->setText("选择文件");  
  42.     choosepath->setMaximumHeight(30);  
  43.     choosepath->setMinimumHeight(30);  
  44.     choosepath->setContentsMargins(0,0,0,0);  
  45.   
  46.     hLayout_one->addWidget(filepath);  
  47.     hLayout_one->addStretch();  
  48.     hLayout_one->addWidget(choosepath);  
  49.     hLayout_one->setContentsMargins(0,0,0,0);  
  50.   
  51.     QHBoxLayout *hLayout_two=new QHBoxLayout(this);  
  52.   
  53.     bookface->setIconSize(QSize(150,150));  
  54.   
  55.     QVBoxLayout *vLayout_bookinfo=new QVBoxLayout(this);  
  56.   
  57.     QHBoxLayout *hLayout_two_one=new QHBoxLayout(this);  
  58.     QLabel *static_writer=new QLabel("作者",this);  
  59.     writername->setFixedSize(100,30);  
  60.     hLayout_two_one->addWidget(static_writer);  
  61.     hLayout_two_one->addStretch();  
  62.     hLayout_two_one->addWidget(writername);  
  63.   
  64.   
  65.     QHBoxLayout *hLayout_two_two=new QHBoxLayout(this);  
  66.     QLabel *static_bookname=new QLabel("书名",this);  
  67.     bookname->setFixedSize(100,30);  
  68.     hLayout_two_two->addWidget(static_bookname);  
  69.     hLayout_two_two->addStretch();  
  70.     hLayout_two_two->addWidget(bookname);  
  71.   
  72.     QHBoxLayout *hLayout_two_three=new QHBoxLayout(this);  
  73.     QLabel *static_publisher=new QLabel("出版社",this);  
  74.     publisher->setFixedSize(100,30);  
  75.     hLayout_two_three->addWidget(static_publisher);  
  76.     hLayout_two_three->addStretch();  
  77.     hLayout_two_three->addWidget(publisher);  
  78.   
  79.   
  80.     vLayout_bookinfo->addLayout(hLayout_two_one);  
  81.     vLayout_bookinfo->addLayout(hLayout_two_two);  
  82.     vLayout_bookinfo->addLayout(hLayout_two_three);  
  83.   
  84.     hLayout_two->addWidget(bookface);  
  85.     hLayout_two->addStretch();  
  86.     hLayout_two->addLayout(vLayout_bookinfo);  
  87.   
  88.     aboutbook =new QLineEdit(this);  
  89.     saveButton=new QPushButton(this);  
  90.     saveButton->setText("保存文件");  
  91.     saveButton->setMinimumWidth(this->width()*0.8);  
  92.     mainLayout->addLayout(hLayout_one);  
  93.     mainLayout->addStretch();  
  94.     mainLayout->addLayout(hLayout_two);  
  95.     mainLayout->addWidget(saveButton);  
  96.     mainLayout->addWidget(aboutbook);  
  97.     setLayout(mainLayout);  
  98.   
  99.   
  100.   
  101. }  
  102.   
  103. void UpLoadFrame::doChoosepathbutton()  
  104. {  
  105.     fileName =QFileDialog::getOpenFileName(this,tr("打开文件"));  
  106.     filepath->setText(fileName);  
  107.     QFileInfo info(fileName);  
  108.     bookname->setText(info.baseName());  
  109. }  
  110.   
  111. void UpLoadFrame::doSaveButton()  
  112. {  
  113.     QFile file(fileName);  
  114.     QFileInfo info(fileName);  
  115.     QString newpath=QString("G:/Reader_Resource")+QString("/");  
  116.   
  117.    //------------------------创建对应书籍的目录------------------------//  
  118.     QDir dir;  
  119.     dir.cd(newpath);  
  120.     dir.mkdir(info.baseName());  
  121.     newpath+=info.baseName();  
  122.     //----------------读取图书的信息并保存在对应的目录下-----------------//  
  123.   
  124.   
  125.     QFile bookinfo(newpath+"/"+info.baseName()+".txt");  
  126.     QFileInfo tmp(bookinfo);  
  127.     aboutbook->setText(tmp.absoluteFilePath());  
  128.     if( bookinfo.open(QFile::ReadWrite))  
  129.     {  
  130.   
  131.         QTextStream out(&bookinfo);  
  132.         out<<writername->text()<<"\r\n"<<endl;  
  133.         out<<bookname->text()<<"\r\n"<<endl;  
  134.         out<<publisher->text()<<"\r\n"<<endl;  
  135.     }  
  136.   
  137.     bookinfo.setFileName(info.baseName()+".txt");  
  138.     bookinfo.close();  
  139.     //--------------------------------------------------------------//  
  140.   
  141.   
  142.     newpath+=QString("/");  
  143.   
  144.     //----------------------复制图片封面信息------------------------//  
  145.     QFile picfile(picpath);  
  146.     QFileInfo picinfo(picfile);  
  147.     QString temptopicpath=newpath;  
  148.     temptopicpath+=picinfo.fileName();  
  149.     picfile.copy(temptopicpath);  
  150.     picfile.close();  
  151.   
  152.     //-------------------------复制文件到相应目录------------------------//  
  153.     newpath+=info.fileName();  
  154.     file.copy(newpath);  
  155.     file.close();  
  156.   
  157.   
  158.   
  159.     QFile checkpath(newpath);  
  160.     if(checkpath.open(QFile::ReadOnly))  
  161.     {  
  162.         QMessageBox msgBox;  
  163.         msgBox.setText("文件复制成功");  
  164.         msgBox.setStandardButtons(QMessageBox::Ok);  
  165.         msgBox.exec();  
  166.     }  
  167.     checkpath.close();  
  168.   
  169.   
  170.   
  171.   
  172. }  
  173.   
  174. void UpLoadFrame::doBookFaceClicked()  
  175. {  
  176.     picpath=QFileDialog::getOpenFileName(this,"指定封面");  
  177.     bookface->setIcon(QIcon(picpath));  
  178. }  

博客原文地址:http://blog.csdn.net/mao19931004/article/details/51501200 



0 0