文件系统交互实现

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

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

相关推荐
青 春 记 忆12 分钟前
LeetCode 155. 最小栈|Python 解法详解
开发语言·python·leetcode
论迹复利3 小时前
TF-M SPE Mailbox IPC 交互流程与 Cache 维护详解
交互·bt·psa
剩下了什么9 小时前
float32 与 float64 精度陷阱:如何在 Go 中避免错误使用
开发语言·后端·golang
指尖时光.9 小时前
Three.js 超全入门综合案例
开发语言·javascript·ecmascript
暴力求解10 小时前
Linux网络---传输层协议TCP(二)
linux·服务器·开发语言·网络·tcp/ip
吴声子夜歌10 小时前
Java面试题——基础(一)
java·开发语言
别动我齐刘海10 小时前
机器人运动控制学习2——基础进阶
c++·人工智能·神经网络·学习·目标检测·机器学习·机器人
小庞在加油10 小时前
WinDbg实战:QT/跨平台项目死锁与无响应问题排查指南
开发语言·qt·windbg·工具
Vae_Mars11 小时前
C#中的delegate委托
开发语言·c#
傻啦嘿哟11 小时前
英雄联盟皮肤爬虫:爬取全皮肤价格与特效,做比价工具
java·c++·爬虫