文件系统交互实现

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

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

相关推荐
TTGGGFF22 分钟前
Supertonic 部署与使用全流程保姆级指南(附已部署镜像)
开发语言·python
木木木一26 分钟前
Rust学习记录--C7 Package, Crate, Module
开发语言·学习·rust
love530love26 分钟前
升级到 ComfyUI Desktop v0.7.0 版本后启动日志报 KeyError: ‘tensorrt‘ 错误解决方案
开发语言·windows·python·pycharm·virtualenv·comfyui·comfyui desktop
Evand J1 小时前
【MATLAB例程】【空地协同】UAV辅助的UGV协同定位,无人机辅助地面无人车定位,带滤波,附MATLAB代码下载链接
开发语言·matlab·无人机·无人车·uav·协同定位·ugv
chao1898441 小时前
基于MATLAB实现多变量高斯过程回归(GPR)
开发语言·matlab·回归
ytttr8737 小时前
隐马尔可夫模型(HMM)MATLAB实现范例
开发语言·算法·matlab
天远Date Lab7 小时前
Python实战:对接天远数据手机号码归属地API,实现精准用户分群与本地化运营
大数据·开发语言·python
listhi5207 小时前
基于Gabor纹理特征与K-means聚类的图像分割(Matlab实现)
开发语言·matlab
qq_433776427 小时前
【无标题】
开发语言·php
会周易的程序员8 小时前
多模态AI 基于工业级编译技术的PLC数据结构解析与映射工具
数据结构·c++·人工智能·单例模式·信息可视化·架构