文件系统交互实现

发布于:2025-05-17 ⋅ 阅读:(20) ⋅ 点赞:(0)

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

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


网站公告

今日签到

点亮在社区的每一天
去签到