Qt文件和目录相关操作

1.相关说明

QCoreApplication类、QFile类、QDir、QTemporaryDir类、QTemporaryFile类、QFileSystemWatcher类的相关函数

2.相关界面

Qt文件和目录相关操作_第1张图片

3.相关代码

#include "dialog.h"
#include "ui_dialog.h"
#include 
#include 
#include 
#include 

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    fileWatcher = NULL;
}

Dialog::~Dialog()
{
    delete ui;
}

// 打开一个文件
void Dialog::on_btnOpenFile_clicked()
{
    QString curDir = QCoreApplication::applicationDirPath();
    QString aFile = QFileDialog::getOpenFileName(this, "打开一个文件", curDir, "(*.cpp *.h *.c);;(*.*)");
    ui->lineEditFile->setText(aFile);
}
// 打开一个目录
void Dialog::on_btnOpenDir_clicked()
{
    QString curDir = QCoreApplication::applicationDirPath();
    QString aDir = QFileDialog::getExistingDirectory(this, "选择一个目录", curDir);
    ui->lineEditDir->setText(aDir);
}
// 清除文本框
void Dialog::on_btnClear_clicked()
{
    ui->plainTextEdit->clear();
}

void Dialog::showBtnInfo(QObject *btn)
{
    QPushButton *theBtn = static_cast(btn);
    ui->plainTextEdit->appendPlainText(theBtn->text());
    ui->plainTextEdit->appendPlainText(theBtn->toolTip()+":");
}

// applicationDirPath()
void Dialog::on_btnApplicationDirPath_clicked()
{
    showBtnInfo(sender());
    QString str = QCoreApplication::applicationDirPath();
    ui->plainTextEdit->appendPlainText(str);
}
// applicationFilePath()
void Dialog::on_btnApplicationFilePath_clicked()
{
    showBtnInfo(sender());
    QString str = QCoreApplication::applicationFilePath();
    ui->plainTextEdit->appendPlainText(str);
}
// applicationName()
void Dialog::on_btnApplicationName_clicked()
{
    showBtnInfo(sender());
    // QCoreApplication::setApplicationName("My App");
    QString str = QCoreApplication::applicationName();
    ui->plainTextEdit->appendPlainText(str);
}
// setApplicationName(QString)
void Dialog::on_btnSetApplicationName_clicked()
{
    showBtnInfo(sender());
    QCoreApplication::setApplicationName("My App");
    QString str = QCoreApplication::applicationName();
    ui->plainTextEdit->appendPlainText(str);
}
// libraryPaths()
void Dialog::on_btnLibraryPaths_clicked()
{
    showBtnInfo(sender());
    QStringList paths = QCoreApplication::libraryPaths();
    for(auto &path : paths){
        ui->plainTextEdit->appendPlainText(path);
    }
}
// organizationName()
void Dialog::on_btnOrganiazationName_clicked()
{
    showBtnInfo(sender());
    QCoreApplication::setOrganizationName("My Organization");
    QString orgName = QCoreApplication::organizationName();
    ui->plainTextEdit->appendPlainText(orgName);
}
// exit()
void Dialog::on_btnExit_clicked()
{
    QCoreApplication::exit();
}
// QFile::copy
void Dialog::on_btnQFileCopy_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString newFile = fileInfo.path()+"/"+fileInfo.baseName()+"--副本."+fileInfo.suffix();
    QFile::copy(sourceFile, newFile);
    ui->plainTextEdit->appendPlainText("源文件:"+sourceFile);
    ui->plainTextEdit->appendPlainText("副本:"+newFile);
}
// QFile::exists
void Dialog::on_btnQFileExists_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    if(QFile::exists(sourceFile)){
        ui->plainTextEdit->appendPlainText(sourceFile + ":存在");
    } else {
        ui->plainTextEdit->appendPlainText(sourceFile + ":不存在");
    }

}
// QFile::remove
void Dialog::on_btnRemove_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    if(QFile::remove(sourceFile)){
        ui->plainTextEdit->appendPlainText("remove "+sourceFile+" 成功");
    } else {
        ui->plainTextEdit->appendPlainText("remove "+sourceFile+" 失败");
    }
}
// QFile::rename
void Dialog::on_btnQFileRename_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString newFile = fileInfo.path()+"/"+fileInfo.baseName()+"--新名称."+fileInfo.suffix();
    if(QFile::rename(sourceFile, newFile)){
        ui->plainTextEdit->appendPlainText("旧名称:"+sourceFile);
        ui->plainTextEdit->appendPlainText("新名称:"+newFile);
    } else {
        ui->plainTextEdit->appendPlainText("修改 "+sourceFile+" 失败");
    }
}
// QFile::moveToTrash
void Dialog::on_btnQFileMoveToTrash_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    if(QFile::exists(sourceFile) && QFile::moveToTrash(sourceFile)){
        ui->plainTextEdit->appendPlainText("删除文件到垃圾箱:"+sourceFile);
    } else {
        ui->plainTextEdit->appendPlainText("文件不存在");
    }
}
// file.copy
void Dialog::on_btnCopy_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString newFile = fileInfo.path()+"/"+fileInfo.baseName()+"--副本."+fileInfo.suffix();
    QFile file(sourceFile);
    // QFile::copy(sourceFile, newFile);
    if(file.copy(newFile)){
        ui->plainTextEdit->appendPlainText("源文件:"+sourceFile);
        ui->plainTextEdit->appendPlainText("副本:"+newFile);
    } else {
        ui->plainTextEdit->appendPlainText("复制文件失败:"+sourceFile);
    }

}
// file.exists
void Dialog::on_btnExists_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFile file(sourceFile);
    if(file.exists()){
        ui->plainTextEdit->appendPlainText(sourceFile + ":存在");
    } else {
        ui->plainTextEdit->appendPlainText(sourceFile + ":不存在");
    }
}
// file.remove
void Dialog::on_btnRemove_2_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFile file(sourceFile);
    if(file.remove()){
        ui->plainTextEdit->appendPlainText("remove "+sourceFile+" 成功");
    } else {
        ui->plainTextEdit->appendPlainText("remove "+sourceFile+" 失败");
    }
}
// file.rename
void Dialog::on_btnRename_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString newFile = fileInfo.path()+"/"+fileInfo.baseName()+"--新名称."+fileInfo.suffix();
    QFile file(sourceFile);
    if(file.rename(newFile)){
        ui->plainTextEdit->appendPlainText("旧名称:"+sourceFile);
        ui->plainTextEdit->appendPlainText("新名称:"+newFile);
    } else {
        ui->plainTextEdit->appendPlainText("修改 "+sourceFile+" 失败");
    }
}
// file.moveToTrash
void Dialog::on_btnMoveToTrash_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFile file(sourceFile);
    if(file.exists() && file.moveToTrash()){
        ui->plainTextEdit->appendPlainText("删除文件到垃圾箱:"+sourceFile);
    } else {
        ui->plainTextEdit->appendPlainText("文件不存在");
    }
}
// absoluteFilePath()
void Dialog::on_btnAbsoluteFilePath_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString absfilePath = fileInfo.absoluteFilePath();
    ui->plainTextEdit->appendPlainText(absfilePath);
}
// absolutePath()
void Dialog::on_btnAbsolutePath_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString absfilePath = fileInfo.absolutePath();
    ui->plainTextEdit->appendPlainText(absfilePath);
}
// fileInfo.fileName()
void Dialog::on_btnFileName_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString fileName = fileInfo.fileName();
    ui->plainTextEdit->appendPlainText(fileName);
}
// fileInfo.filePath()
void Dialog::on_btnFilePath_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString filePath = fileInfo.filePath();
    ui->plainTextEdit->appendPlainText(filePath);
}
// fileInfo.size()
void Dialog::on_btnSize_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString fileSize = QString::number(fileInfo.size());
    ui->plainTextEdit->appendPlainText(fileSize+"Bytes");
}
// fileInfo.path()
void Dialog::on_btnPath_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString path = fileInfo.path();
    ui->plainTextEdit->appendPlainText(path);
}
// fileInfo.baseName()
void Dialog::on_btnBaseName_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString baseName = fileInfo.baseName();
    ui->plainTextEdit->appendPlainText(baseName);
}
// fileInfo.completeBaseName()
void Dialog::on_btnCompleteBaseName_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString completeBaseName = fileInfo.completeBaseName();
    ui->plainTextEdit->appendPlainText(completeBaseName);
}
// fileInfo.suffix()
void Dialog::on_btnSuffix_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString suffix = fileInfo.suffix();
    ui->plainTextEdit->appendPlainText(suffix);
}
// fileInfo.completeSuffix()
void Dialog::on_btnCompleteSuffix_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    QString completeSuffix = fileInfo.completeSuffix();
    ui->plainTextEdit->appendPlainText(completeSuffix);
}
// fileInfo.isDir()
void Dialog::on_btnIsDir_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    if(fileInfo.isDir()){
        ui->plainTextEdit->appendPlainText("目录");
    } else {
        ui->plainTextEdit->appendPlainText("非目录");
    }
}
// fileInfo.isFile()
void Dialog::on_btnIsFile_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    if(fileInfo.isFile()){
        ui->plainTextEdit->appendPlainText("文件");
    } else {
        ui->plainTextEdit->appendPlainText("非文件");
    }
}
// fileInfo.isExecutable()
void Dialog::on_btnIsExecutable_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    if(fileInfo.isExecutable()){
        ui->plainTextEdit->appendPlainText("执行文件");
    } else {
        ui->plainTextEdit->appendPlainText("非执行文件");
    }
}
// fileInfo.birthTime()
void Dialog::on_btnBirthTime_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    if(fileInfo.exists()){
        ui->plainTextEdit->appendPlainText("创建时间:"+fileInfo.birthTime().toString("yyyy-MM-dd hh:mm:ss"));
    }
}
// fileInfo.lastModified()
void Dialog::on_btnLastModified_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    if(fileInfo.exists()){
        ui->plainTextEdit->appendPlainText("最后修改时间:"+fileInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss"));
    }
}
// fileInfo.lastRead()
void Dialog::on_btnLastRead_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    if(fileInfo.exists()){
        ui->plainTextEdit->appendPlainText("最后读取时间:"+fileInfo.lastRead().toString("yyyy-MM-dd hh:mm:ss"));
    }
}
// QFileInfo::exists(sourceFile)
void Dialog::on_btnStaticExists_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    if(QFileInfo::exists(sourceFile)){
        ui->plainTextEdit->appendPlainText("文件存在");
    } else {
        ui->plainTextEdit->appendPlainText("文件不存在");
    }
}
// fileInfo.exists()
void Dialog::on_btnDynamicExists_clicked()
{
    showBtnInfo(sender());
    QString sourceFile = ui->lineEditFile->text();
    QFileInfo fileInfo(sourceFile);
    if(fileInfo.exists()){
        ui->plainTextEdit->appendPlainText("文件存在");
    } else {
        ui->plainTextEdit->appendPlainText("文件不存在");
    }
}
// QDir::tempPath()
void Dialog::on_btnTempPath_clicked()
{
    showBtnInfo(sender());
    ui->plainTextEdit->appendPlainText("tempPath:"+QDir::tempPath());
}
// QDir::rootPath()
void Dialog::on_btnRootPath_clicked()
{
    showBtnInfo(sender());
    ui->plainTextEdit->appendPlainText("rootPath:"+QDir::rootPath());
}
// QDir::homePath()
void Dialog::on_btnHomePath_clicked()
{
    showBtnInfo(sender());
    ui->plainTextEdit->appendPlainText("homePath:"+QDir::homePath());
}
// QDir::drives()
void Dialog::on_btnDrives_clicked()
{
    showBtnInfo(sender());
    QFileInfoList filelist = QDir::drives();
    for(auto &fileInfo : filelist){
        ui->plainTextEdit->appendPlainText(fileInfo.path());
    }
}
// QDir::currentPath()
void Dialog::on_btnCurrentPath_clicked()
{
    showBtnInfo(sender());
    ui->plainTextEdit->appendPlainText("currentPath:"+QDir::currentPath());
}

// QDir::setCurrent(ui->lineEditDir->text())
void Dialog::on_btnSetCurrent_clicked()
{
    showBtnInfo(sender());
    QDir::setCurrent(ui->lineEditDir->text());
    ui->plainTextEdit->appendPlainText("currentPath:"+QDir::currentPath());
}
// dir.mkdir(subDir)
void Dialog::on_btnMkdir_clicked()
{
    showBtnInfo(sender());
    QString subDir = "subdir";
    QDir dir(ui->lineEditDir->text());
    if(dir.mkdir(subDir)){
        ui->plainTextEdit->appendPlainText("创建成功:"+subDir);
    } else {
        ui->plainTextEdit->appendPlainText("创建失败:"+subDir);
    }
}
// dir.rmdir(subDir)
void Dialog::on_btnRmdir_clicked()
{
    showBtnInfo(sender());
    QString subDir = "subdir";
    QDir dir(ui->lineEditDir->text());
    if(dir.rmdir(subDir)){
        ui->plainTextEdit->appendPlainText("删除成功:"+subDir);
    } else {
        ui->plainTextEdit->appendPlainText("删除失败:"+subDir);
    }
}
// dir.remove(fileName)
void Dialog::on_btnRemove_3_clicked()
{
    showBtnInfo(sender());
    QString fileName = ui->lineEditFile->text();
    QDir dir(ui->lineEditDir->text());
    if(dir.remove(fileName)){
        ui->plainTextEdit->appendPlainText("删除成功:"+fileName);
    } else {
        ui->plainTextEdit->appendPlainText("删除失败:"+fileName);
    }
}
// dir.rename(fileName, newFile)
void Dialog::on_btnDirRename_clicked()
{
    showBtnInfo(sender());
    QString fileName = ui->lineEditFile->text();
    QFileInfo fileInfo(fileName);
    QDir dir(ui->lineEditDir->text());
    QString newFile = fileInfo.path()+"/"+fileInfo.baseName()+".XYZ";
    if(dir.rename(fileName, newFile)){
        ui->plainTextEdit->appendPlainText("源文件:"+fileName);
        ui->plainTextEdit->appendPlainText("修改后的文件:"+newFile);
    } else {
        ui->plainTextEdit->appendPlainText("修改失败");
    }
}
// lastDir.setPath(aDir)
void Dialog::on_btnSetPath_clicked()
{
    showBtnInfo(sender());
    QString curDir = QDir::currentPath();
    QDir lastDir(curDir);
    ui->plainTextEdit->appendPlainText("当前目录:"+lastDir.absolutePath());
    QString aDir = QFileDialog::getExistingDirectory(this, "选择一个目录",curDir);
    if(aDir.isEmpty()){
        return;
    }
    ui->lineEditDir->setText(aDir);
    lastDir.setPath(aDir);
    ui->plainTextEdit->appendPlainText("选择后的目录:"+lastDir.absolutePath());
}
// aDir.removeRecursively()
void Dialog::on_btnRemoveRecursively_clicked()
{
    showBtnInfo(sender());
    QDir aDir(ui->lineEditDir->text());
    if(aDir.removeRecursively()){
        ui->plainTextEdit->appendPlainText("递归删除成功");
    } else {
        ui->plainTextEdit->appendPlainText("递归删除失败");
    }
}
// aDir.absoluteFilePath(ui->lineEditFile->text())
void Dialog::on_btnAbsoluteFilePath_2_clicked()
{
    showBtnInfo(sender());
    QDir aDir(ui->lineEditDir->text());
    QString str = aDir.absoluteFilePath(ui->lineEditFile->text());
    ui->plainTextEdit->appendPlainText(str);
}
// aDir.absolutePath()
void Dialog::on_btnDirAbsolutePath_clicked()
{
    showBtnInfo(sender());
    QDir aDir(ui->lineEditDir->text());
    ui->plainTextEdit->appendPlainText(aDir.absolutePath());
}
// aDir.canonicalPath()
void Dialog::on_btnCanonicalPath_clicked()
{
    showBtnInfo(sender());
    QDir aDir(ui->lineEditDir->text());
    ui->plainTextEdit->appendPlainText(aDir.canonicalPath());
}
// aDir.filePath(ui->lineEditFile->text())
void Dialog::on_btnDirFilePath_clicked()
{
    showBtnInfo(sender());
    QDir aDir(ui->lineEditDir->text());
    QString filePath = aDir.filePath(ui->lineEditFile->text());
    ui->plainTextEdit->appendPlainText(filePath);
}
// aDir.path()
void Dialog::on_btnDirPath_clicked()
{
    showBtnInfo(sender());
    QDir aDir(ui->lineEditDir->text());
    ui->plainTextEdit->appendPlainText(aDir.path());
}
// aDir.dirName()
void Dialog::on_btnDirName_clicked()
{
    showBtnInfo(sender());
    QDir aDir(ui->lineEditDir->text());
    ui->plainTextEdit->appendPlainText(aDir.dirName());
}
// aDir.exists()
void Dialog::on_btnDirExists_clicked()
{
    showBtnInfo(sender());
    QDir aDir(ui->lineEditDir->text());
    if(aDir.exists()){
        ui->plainTextEdit->appendPlainText("存在:"+aDir.dirName());
    } else {
        ui->plainTextEdit->appendPlainText("不存在:"+aDir.dirName());
    }

}
// aDir.isEmpty()
void Dialog::on_btnDirIsEmpty_clicked()
{
    showBtnInfo(sender());
    QDir aDir(ui->lineEditDir->text());
    if(aDir.isEmpty()){
        ui->plainTextEdit->appendPlainText("空文件夹:"+aDir.dirName());
    } else {
        ui->plainTextEdit->appendPlainText("非空文件夹:"+aDir.dirName());
    }
}
// aDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)
void Dialog::on_btnDirEntryList_clicked()
{
    showBtnInfo(sender());
    QDir aDir(ui->lineEditDir->text());
    QStringList strList = aDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
    ui->plainTextEdit->appendPlainText(aDir.dirName()+"目录下的所有目录:");
    for(QString &dir : strList){
        ui->plainTextEdit->appendPlainText(dir);
    }
}
// aDir.entryList(QDir::Files)
void Dialog::on_btnFileEntryList_clicked()
{
    showBtnInfo(sender());
    QDir aDir(ui->lineEditDir->text());
    QStringList strList = aDir.entryList(QDir::Files);
    ui->plainTextEdit->appendPlainText(aDir.dirName()+"目录下的所有文件:");
    for(QString &dir : strList){
        ui->plainTextEdit->appendPlainText(dir);
    }
}
// 1.在系统临时目录下创建临时文件夹
void Dialog::on_btnCreateTmpDir_clicked()
{
    showBtnInfo(sender());
    ui->plainTextEdit->appendPlainText("QDir::tempPath()="+QDir::tempPath());
    QTemporaryDir dir;
    dir.setAutoRemove(true);
    ui->plainTextEdit->appendPlainText(dir.path());
}
// 1.在系统临时目录下创建临时文件
void Dialog::on_btnCreateTmpFile_clicked()
{
    showBtnInfo(sender());
    ui->plainTextEdit->appendPlainText("QDir::tempPath()="+QDir::tempPath());
    QTemporaryFile aFile;
    aFile.setAutoRemove(true);
    aFile.open();
    ui->plainTextEdit->appendPlainText(aFile.fileName());
    aFile.close();
}
// 2.在指定目录下创建临时文件夹
void Dialog::on_btnCreateTmpDir_2_clicked()
{
    showBtnInfo(sender());
    QString specDir = ui->lineEditDir->text();
    ui->plainTextEdit->appendPlainText("指定目录="+specDir);
    QTemporaryDir dir(specDir + "/TempDir_XXX");
    dir.setAutoRemove(false);
    ui->plainTextEdit->appendPlainText(dir.path());
}
// 2.在指定目录下创建临时文件
void Dialog::on_btnCreateTmpFile_2_clicked()
{
    showBtnInfo(sender());
    QString specDir = ui->lineEditDir->text();
    ui->plainTextEdit->appendPlainText("指定目录="+specDir);
    QTemporaryFile aFile(specDir+"/TempFile_XXX");
    aFile.setAutoRemove(false);
    aFile.open();
    ui->plainTextEdit->appendPlainText(aFile.fileName());
    aFile.close();
}
// 3.在当前目录下创建临时文件夹
void Dialog::on_btnCreateCurTmpDir_clicked()
{
    showBtnInfo(sender());
    ui->plainTextEdit->appendPlainText("当前路径="+QDir::currentPath());
    QTemporaryDir dir("SubDir_XXX");
    dir.setAutoRemove(false);
    ui->plainTextEdit->appendPlainText(dir.path());
}
// 3.在当前目录下创建临时文件
void Dialog::on_btnCreateCurTmpFile_clicked()
{
    showBtnInfo(sender());
    ui->plainTextEdit->appendPlainText("当前路径="+QDir::currentPath());
    QTemporaryFile aFile("CurTempFile_XXX.temp");
    aFile.setAutoRemove(false);
    aFile.open();
    ui->plainTextEdit->appendPlainText(aFile.fileName());
    aFile.close();
}
void Dialog::do_directoryChanged(const QString &path)
{
    ui->plainTextEdit->appendPlainText(path + "目录发生了变化");
}

void Dialog::do_fileChanged(const QString &path)
{
    ui->plainTextEdit->appendPlainText(path+"文件发生了变化");
}
// addPath()并开始监视
void Dialog::on_btnAddPathWatcher_clicked()
{
    showBtnInfo(sender());
    if(fileWatcher == NULL){
        fileWatcher = new QFileSystemWatcher(this);
    }
    fileWatcher->addPath(ui->lineEditDir->text());
    fileWatcher->addPath(ui->lineEditFile->text());
    connect(fileWatcher, &QFileSystemWatcher::directoryChanged, this, &Dialog::do_directoryChanged);
    connect(fileWatcher, &QFileSystemWatcher::fileChanged, this, &Dialog::do_fileChanged);
}
// removePath()并停止监视
void Dialog::on_btnRemovePathWatcher_clicked()
{
    showBtnInfo(sender());
    fileWatcher->removePath(ui->lineEditDir->text());
    fileWatcher->removePath(ui->lineEditFile->text());
    disconnect(fileWatcher);
}
// directories()
void Dialog::on_btnDirectories_clicked()
{
    showBtnInfo(sender());
    for(auto &item : fileWatcher->directories()){
        ui->plainTextEdit->appendPlainText(item);
    }
}
// files()
void Dialog::on_btnFiles_clicked()
{
    showBtnInfo(sender());
    for(QString &item : fileWatcher->files()){
        ui->plainTextEdit->appendPlainText(item);
    }
}

你可能感兴趣的:(Qt6,qt,开发语言,qt6.3)