Qt(五)--- 文件操作以及实现文件的复制操作并且在目的目录创建相应文件夹

来源:互联网 发布:mac系统ps怎么装插件 编辑:程序博客网 时间:2024/05/13 20:36

最近想做一个局域网的图书管理的一个应用,希望能够管理本地的电子书资源,同时分享给在同一个局域网的用户们。因此在本地需要建立一个图书的管理目录出来(暂时是这样想的),因此需要对电脑上的资源进行统一的移动和复制,同时将可能以后需要的信息进行保存下来,在Qt中可能设计到相关内容的包含:

  • QFile 的使用
  • QDir 的使用
  • QFileInfo的使用
  • QFileDialog的使用
QFile类提供了读取文件和操作文件的接口,可以读写文本文件以及二进制文件,以及资源文件。通常常用的函数包含open()close()函数的使用。

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

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_OBJECTpublic:    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
重点是实现三个槽。首先是第一个槽,doChoosepathbutton(),通过店家choosepath按键,打开一个选择文件的对话框选择文件,在该函数中实现文件路径的读取,通过文件的读取,自动设置书名。实现内容如下:

void UpLoadFrame::doChoosepathbutton(){    fileName =QFileDialog::getOpenFileName(this,tr("打开文件"));    filepath->setText(fileName);    QFileInfo info(fileName);    bookname->setText(info.baseName());}
其中一个关键的只是点是关于QFileDialog的知识点:

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("/");    //----------------------复制图片封面信息------------------------//    QFile picfile(picpath);    QFileInfo picinfo(picfile);    QString temptopicpath=newpath;    temptopicpath+=picinfo.fileName();    picfile.copy(temptopicpath);    picfile.close();    //-------------------------复制文件到相应目录------------------------//    newpath+=info.fileName();    file.copy(newpath);    file.close();    QFile checkpath(newpath);    if(checkpath.open(QFile::ReadOnly))    {        QMessageBox msgBox;        msgBox.setText("文件复制成功");        msgBox.setStandardButtons(QMessageBox::Ok);        msgBox.exec();    }    checkpath.close();}
对以上的代码实现进行简答的解释:

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

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

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

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

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

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



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

完成的实现代码如下:

#include "uploadframe.h"#include<QLabel>#include<QIcon>#include<QLineEdit>#include<QPushButton>#include<QTextBrowser>#include<QToolButton>#include<QHBoxLayout>#include<QVBoxLayout>#include<QFile>#include<QFIleDialog>#include<QFileInfo>#include<QMessageBox>#include<QDir>#include<QTextStream>UpLoadFrame::UpLoadFrame(QWidget *parent) : QWidget(parent){    filepath=new QLineEdit(this);    choosepath=new QPushButton(this);    bookface=new QToolButton();    writername=new QLineEdit(this);    publisher=new QLineEdit(this);    bookname=new QLineEdit(this);    aboutbook=new QLineEdit(this);    init();    connect(choosepath,SIGNAL(clicked(bool)),this,SLOT(doChoosepathbutton()));    connect(saveButton,SIGNAL(clicked(bool)),this,SLOT(doSaveButton()));    connect(bookface,SIGNAL(clicked(bool)),this,SLOT(doBookFaceClicked()));    setFixedSize(400,300);}void UpLoadFrame::init(){    QVBoxLayout *mainLayout=new QVBoxLayout(this);    QHBoxLayout *hLayout_one=new QHBoxLayout(this);    filepath->setMinimumSize(250,20);    filepath->setMaximumSize(250,20);    filepath->setContentsMargins(0,0,0,0);    choosepath->setText("选择文件");    choosepath->setMaximumHeight(30);    choosepath->setMinimumHeight(30);    choosepath->setContentsMargins(0,0,0,0);    hLayout_one->addWidget(filepath);    hLayout_one->addStretch();    hLayout_one->addWidget(choosepath);    hLayout_one->setContentsMargins(0,0,0,0);    QHBoxLayout *hLayout_two=new QHBoxLayout(this);    bookface->setIconSize(QSize(150,150));    QVBoxLayout *vLayout_bookinfo=new QVBoxLayout(this);    QHBoxLayout *hLayout_two_one=new QHBoxLayout(this);    QLabel *static_writer=new QLabel("作者",this);    writername->setFixedSize(100,30);    hLayout_two_one->addWidget(static_writer);    hLayout_two_one->addStretch();    hLayout_two_one->addWidget(writername);    QHBoxLayout *hLayout_two_two=new QHBoxLayout(this);    QLabel *static_bookname=new QLabel("书名",this);    bookname->setFixedSize(100,30);    hLayout_two_two->addWidget(static_bookname);    hLayout_two_two->addStretch();    hLayout_two_two->addWidget(bookname);    QHBoxLayout *hLayout_two_three=new QHBoxLayout(this);    QLabel *static_publisher=new QLabel("出版社",this);    publisher->setFixedSize(100,30);    hLayout_two_three->addWidget(static_publisher);    hLayout_two_three->addStretch();    hLayout_two_three->addWidget(publisher);    vLayout_bookinfo->addLayout(hLayout_two_one);    vLayout_bookinfo->addLayout(hLayout_two_two);    vLayout_bookinfo->addLayout(hLayout_two_three);    hLayout_two->addWidget(bookface);    hLayout_two->addStretch();    hLayout_two->addLayout(vLayout_bookinfo);    aboutbook =new QLineEdit(this);    saveButton=new QPushButton(this);    saveButton->setText("保存文件");    saveButton->setMinimumWidth(this->width()*0.8);    mainLayout->addLayout(hLayout_one);    mainLayout->addStretch();    mainLayout->addLayout(hLayout_two);    mainLayout->addWidget(saveButton);    mainLayout->addWidget(aboutbook);    setLayout(mainLayout);}void UpLoadFrame::doChoosepathbutton(){    fileName =QFileDialog::getOpenFileName(this,tr("打开文件"));    filepath->setText(fileName);    QFileInfo info(fileName);    bookname->setText(info.baseName());}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("/");    //----------------------复制图片封面信息------------------------//    QFile picfile(picpath);    QFileInfo picinfo(picfile);    QString temptopicpath=newpath;    temptopicpath+=picinfo.fileName();    picfile.copy(temptopicpath);    picfile.close();    //-------------------------复制文件到相应目录------------------------//    newpath+=info.fileName();    file.copy(newpath);    file.close();    QFile checkpath(newpath);    if(checkpath.open(QFile::ReadOnly))    {        QMessageBox msgBox;        msgBox.setText("文件复制成功");        msgBox.setStandardButtons(QMessageBox::Ok);        msgBox.exec();    }    checkpath.close();}void UpLoadFrame::doBookFaceClicked(){    picpath=QFileDialog::getOpenFileName(this,"指定封面");    bookface->setIcon(QIcon(picpath));}

0 0
原创粉丝点击