文件系统交互实现

关于之前的搭建看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;
    }
}

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

相关推荐
菜鸟~noob23327 分钟前
【Bonjour】华为Peerium架构深度解析:从冯·诺依曼到百万处理器“一台计算机”,附MATLAB性能仿真【含matlab代码,可直接运行】
开发语言·matlab·华为·peerium
朝朝辞暮i29 分钟前
C++第 2 课
c++
代码中介商35 分钟前
C++ 预约系统实战(三):服务端实现——libevent 事件驱动与业务路由
c++·json·c/s
白远山1 小时前
24小时自助健身房系统开发实战:从需求分析到完整指南
java·开发语言·数据库·数据挖掘·需求分析
布莱克6052 小时前
C++中的内存分区详解
c++
All for pursuit.2 小时前
【链表-9】146.LRU缓存
数据结构·c++·算法·leetcode
shmily麻瓜小菜鸡3 小时前
TDD(测试驱动开发)详解
开发语言·javascript·typescript·node.js·ecmascript
三克的油3 小时前
java-学习1
java·开发语言·学习
j7~3 小时前
【C++微服务项目开发脚手架】(接口篇一)gflags + gtest + spdlog 接口学习笔记
c++·学习·gtest·项目开发·spdlog·gflags·c++项目微服务开发脚手架
星期八不上发条3 小时前
智能指针是什么?使用场景,循环引用解决办法,面试回答
开发语言·c++·stl