文件系统交互实现

关于之前的搭建看QT控件文件系统的实现-CSDN博客,接下来是对本程序的功能完善,我想着是这样设计的,打开一个目录以后,鼠标选中一个项可以是目录,也可以是文件,右键可以出现一个菜单选择操作,比如删除,重命名,新建文件等。

新添头文件

复制代码
#include<QAction>
#include<QMenu>
#include<QString>
#include<QModelIndex>
#include<QFileInfo>
#include<QMessageBox>
#include<QInputDialog>

成员变量

复制代码
    QMenu *menu;//右键菜单
    QAction *deldir;//删除操作
    QAction *newdir;//新建文件夹
    QAction *newfile;//新建文件
    QAction *renamefile;//重命名文件
    QString filepath;//当前操作所在的目录地址
    QModelIndex selectindex;//当前选中的项
    bool hasopen=false;//只有打开过才可以触发菜单

槽函数

复制代码
void onmenu(const QPoint &pos);//右键菜单栏
    void deleteSelect();//删除目录
    void renameselect();//重命名文件
    void createfileincurrentdir();//在当前目录下新建文件
    void createFolderincurrentdir();//在当前目录下新建目录

主程序

在主程序里面首先就是创建菜单栏,创建爱了许多action,但是我这菜单中的选中分为目录和文件,在不同的地方选中可使用的action有些不同,执行的逻辑就是首先判断有没有打开过目录,没有打开过就挂掉,打开过判断当前索引是否有效,如果有效选中的是目录还是文件,如果无效如果是空文件夹下就设置相应的action。具体程序如下

复制代码
void MainWindow::onmenu(const QPoint &pos)
{
    //如果没有打开过目录不触发
    if(hasopen==false){return;}
    //QAction初始化
    deldir=new QAction("删除",this);
    newdir=new QAction("新建文件夹",this);//新建文件夹
    newfile=new QAction("新建文件",this);//新建文件
    renamefile=new QAction("重命名文件",this);//重命名文件
    //获取当前选中项索引
    selectindex=ui->treeView->indexAt(pos);
    //if(!selectindex.isValid()){return;}
    menu->clear();
    filepath=model->filePath(selectindex);

    // 判断是否有效索引
    if (selectindex.isValid()) {
        filepath = model->filePath(selectindex);
        // 根据文件类型添加菜单项
        if (model->isDir(selectindex)) {
            menu->addAction(deldir);
            menu->addAction(newdir);
            menu->addAction(newfile);
        } else {
            menu->addAction(deldir);
            menu->addAction(renamefile);
        }
    }
    // 无效索引时(空文件夹空白处)
    else {
        // 获取当前视图的根目录路径
        filepath = model->rootPath();
        if(filepath.isEmpty()){
            filepath=QDir::homePath();
        }
        // 添加新建操作
        menu->addAction(newdir);
        menu->addAction(newfile);
    }
    connect(deldir,&QAction::triggered,this,&MainWindow::deleteSelect);
    connect(newdir,&QAction::triggered,this,&MainWindow::createFolderincurrentdir);
    connect(newfile,&QAction::triggered,this,&MainWindow::createfileincurrentdir);
    connect(renamefile,&QAction::triggered,this,&MainWindow::renameselect);
//?
     menu->exec(ui->treeView->viewport()->mapToGlobal(pos));
}

然后就是槽函数的具体实现了

复制代码
void MainWindow::deleteSelect()
{
    QString path=model->filePath(selectindex);
    QFileInfo fileinfo(path);
    QString title=fileinfo.isDir()?"删除目录":"删除文件";
    QString question=fileinfo.isDir()?"确定要删除目录和该目录下所有文件?":"确定要删除该文件?";
    QMessageBox::StandardButton reply=QMessageBox::question(this,title,question,QMessageBox::Yes|QMessageBox::No);
    if(reply==QMessageBox::Yes){
        bool success=fileinfo.isDir()?QDir(path).removeRecursively():QFile::remove(path);
        if(!success){
            QMessageBox::critical(this,"错误","删除失败,请检查权限或以管理员身份运行");
        }

    }
}

//重命名文件
void MainWindow::renameselect()
{
    if(model->isDir(selectindex)){return;}

    //弹窗输入新文件名
    bool ok;
    QString newfilename=QInputDialog::getText(this
                                                ,"重命名文件"
                                                ,"请输入新文件名:"
                                                ,QLineEdit::Normal
                                                ,model->fileName(selectindex)
                                                ,&ok);
    //如果修改了并且新的文件名不为空
    if(ok&&!newfilename.isEmpty()){
        QDir dir=model->fileInfo(selectindex).dir();
        QString oldpath=model->filePath(selectindex);
        QString newpath=dir.absoluteFilePath(newfilename);

        if(!QFile::rename(oldpath,newpath)){
            qDebug()<<"重命名失败!";
        }
    }

}

//在当前目录下创建文件
void MainWindow::createfileincurrentdir()
{
    bool ok;
    QString filename=QInputDialog::getText(this
                                             ,"新建文件"
                                             ,"请输入新文件文件名:"
                                             ,QLineEdit::Normal
                                             ,"",
                                             &ok);
    if(!ok||filename.isEmpty()){return;}
    //新建文件
    QFile file(QDir(filepath).filePath(filename));
    if(!file.open(QIODevice::WriteOnly)){
        QMessageBox::critical(this,"错误","创建文件失败,请检查文件名或权限");
        return;
    }
    file.close();
}

//在当前目录下创建文件夹
void MainWindow::createFolderincurrentdir()
{
    bool ok;
    QString dirname=QInputDialog::getText(this
                                            ,"新建目录"
                                            ,"请输入新建的目录名:"
                                            ,QLineEdit::Normal
                                            ,""
                                            ,&ok);
    if(!ok||dirname.isEmpty()){return;}
    QDir dir(filepath);
    if(!dir.mkdir(dirname)){
        QMessageBox::critical(this,"错误","目录创建失败");
        return;
    }
}

源码已上传,大家先看看代码吧,代码都有注释,后面我慢慢完善

相关推荐
冻柠檬飞冰走茶2 分钟前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
FRing0317 分钟前
vue2干货总结7(自定义指令、插槽、单页应用程序、路由的使用(VueRouter))
开发语言·前端·vue.js·vue2
雪落漂泊21 分钟前
C++算法 二分查找篇(上)
开发语言·c++·算法
波力海苔夹心脆6751 小时前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
不会算法的笨小孩2 小时前
异常的使用以及会出现的问题
开发语言·c++·c++11·异常
Hhy_11074 小时前
《C++深度解构06》手撕 string —— 从底层内存管理到深浅拷贝彻底吃透
c语言·开发语言·c++·学习·visual studio
寂寞的船4 小时前
【Unity知识速递】第8期 IL2CPP 托管堆内存碎片与原生堆交互机制深度解析
unity·游戏引擎·交互
starzy19904 小时前
深入理解Scala函数定义:从方法到函数的类型跨越
开发语言·scala
starzy19904 小时前
深入理解Scala递归函数:从栈溢出到尾调用优化
开发语言·scala
三*一4 小时前
Mapbox‑GL JS 多边形折点编辑工具:实现原理、功能拆解与踩坑总结
开发语言·javascript·ecmascript