Qt文件和目录相关操作

news2024/11/28 8:32:53

1.相关说明

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

2.相关界面

3.相关代码

#include "dialog.h"
#include "ui_dialog.h"
#include <QFileDialog>
#include <QTemporaryDir>
#include <QTemporaryFile>
#include <QFileSystemWatcher>

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<QPushButton*>(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);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1399262.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

MySQL综合练习题

一、创建表的素材 CREATE TABLE dept ( deptno INT(2) NOT NULL COMMENT 部门编号, dname VARCHAR (15) COMMENT 部门名称, loc VARCHAR (20) COMMENT 地理位置 ); -- 添加主键 ALTER TABLE dept ADD PRIMARY KEY (deptno); -- 添加数据 INSERT INTO dept (deptno…

nuclei安装;linux上 以及使用教程

kali安装go环境_go1.17 kali安装-CSDN博客Ubuntu完美解决Github网站打不开问题 - 一抹烟霞 - 博客园 (cnblogs.com) All releases - The Go Programming Language 然但是上面两个我似乎都没用到网上的教程 也不适用 一个网不好 一个apt没找到包 然后我先试试了版本 结果 我的…

组件通信方式

组件通信 父子组件通信 单向数据流 属性传递props&#xff08;还有插槽&#xff0c;$attrs非属性&#xff09;/$emit&#xff0c;发布订阅模式 方法也可以作为属性 父子组件渲染生命周期&#xff1a; 获取组件实例。$children、ref&$refs/$parent 祖先和后代 组件和后代通信…

python之粘包/粘包的解决方案

python之粘包/粘包的解决方案 什么是粘包 粘包就是在数据传输过程中有多个数据包被粘连在一起被发送或接受 服务端&#xff1a; import socket import struct# 创建Socket Socket socket.socket(socket.AF_INET, socket.SOCK_STREAM)# 绑定服务器和端口号 servers_addr (…

java打包及上传到私服务

一、准备Maven私服Nexus 添加saas.maven 仓库地址&#xff1a;http://192.168.31.109:8081/repository/saas.maven 二、新建SpringBoot项目com.saas.pdf 添加类&#xff1a;PdfUtil.java package com.saas.pdf;public class PdfUtil {public static void Save(String fileP…

ubuntu20遇到缺少qt4相关库的问题

最近需要做套接字通讯的工作&#xff0c;最好是有一个网络调试软件能够接受或者发送套接字&#xff0c;测试代码能够正常通讯。windows下有很多&#xff0c;但是linux下比较少&#xff0c;使用广泛的是下面这一款。 1、安装 首先从网盘&#xff08;链接: https://pan.baidu.c…

安装conda搭建python环境(保姆级教程)

目录 一、Anaconda简介二、Anaconda安装 2.1 Anaconda下载2.2 Anaconda安装2.3 配置环境变量 三、通过conda配置python环境 3.1 创建并激活虚拟环境3.2 管理虚拟环境 一、Anaconda简介 Anaconda 是专门为了方便使用 Python 进行数据科学研究而建立的一组软件包&#xff0c;…

【设计模式-08】Flyweight享元模式

简要说明 简要的理解&#xff1a;享元模式就是新建一个池(Pool)&#xff0c;该池子(Pool)中有新建好的一堆对象&#xff0c;当需要使用时&#xff0c;从池子(Pool)中直接获取&#xff0c;不用重新新建一个对象。通俗的讲就是&#xff1a;共享元数据。 比如Java中的String就是使…

Python圣诞主题绘图:用turtle库打造冬日奇妙画面【第31篇—python:圣诞节】

文章目录 Python圣诞主题绘图导言代码结构概览详细解析drawlight函数tree函数xzs函数drawsnow函数五角星的绘制 完整代码代码解析总结 Python圣诞主题绘图 导言 圣诞季节是个充满欢乐和创意的时刻。在这个技术博客中&#xff0c;我们将深入探讨如何使用Python的turtle库创建一…

【华为 ICT HCIA eNSP 习题汇总】——题目集4

1、&#xff08;多选&#xff09;网络中出现故障后&#xff0c;管理员通过排查发现某台路由器的配置被修改了&#xff0c;那么管理员应该采取哪些措施来避免这种状况再次发生&#xff1f; A、管理员应该通过配置 ACL 来扩展只有管理员能够登录设备 B、管理员应该在路由的管理端…

Redis原理篇(QuickList)

一.前言&#xff1a;ZipList出现的问题 QuickList的出现是为了解决ZipList所存在的一些问题 1.寻找大内存块&#xff0c;申请内存效率低 ZipList所申请的是连续的内存空间&#xff0c;如果ZipList里面存放的数据过多&#xff0c;就需要一块很大的连续内存&#xff0c;系统需…

【c++笔记】用c++解决一系列质数问题!

质数是c语言和c中比较常见的数学问题&#xff0c;本篇文章将带你走进有关质数的一系列基础问题&#xff0c;其中包含常见的思路总结&#xff0c;本篇文章过后&#xff0c;将会持续更新c算法系列&#xff0c;感兴趣的话麻烦点个关注吧&#xff01; 希望能给您带来帮助&#xff…

新能源智慧充电桩方案:AI视频分析技术如何助力充电桩智能监管?

随着AI人工智能、大数据、云计算等技术快速发展与落地&#xff0c;视频智能分析技术在智慧充电桩场景中的应用也越来越广泛。这种技术能够为充电桩站点提供全方位的监控和管理&#xff0c;提高运营效率&#xff0c;保障充电桩设备的安全和稳定运行。 通过TSINGSEE青犀&触角…

实验五 PLSQL编程

&#x1f57a;作者&#xff1a; 主页 我的专栏C语言从0到1探秘C数据结构从0到1探秘Linux &#x1f618;欢迎关注&#xff1a;&#x1f44d;点赞&#x1f64c;收藏✍️留言 &#x1f3c7;码字不易&#xff0c;你的&#x1f44d;点赞&#x1f64c;收藏❤️关注对我真的很重要&…

Arduino开发实例-MTH02温湿度传感器驱动

MTH02温湿度传感器驱动 文章目录 MTH02温湿度传感器驱动1、MTH02温湿度传感器介绍2、硬件准备及接线3、代码实现1、MTH02温湿度传感器介绍 市场上的温湿度传感器在价格、精度和测量范围等方面种类繁多。 MTH02O 是这些传感器中最小的一种。 该传感器使用数字引脚传输温度和湿度…

领略指针之妙

&#x1d649;&#x1d65e;&#x1d658;&#x1d65a;!!&#x1f44f;&#x1f3fb;‧✧̣̥̇‧✦&#x1f44f;&#x1f3fb;‧✧̣̥̇‧✦ &#x1f44f;&#x1f3fb;‧✧̣̥̇:Solitary-walk ⸝⋆ ━━━┓ - 个性标签 - &#xff1a;来于“云”的“羽球人”。…

mac-hadoop3.3.6 源码构建以及踩坑记录

1. 为什么需要构建源码 因为hadoop的可执行文件 是在专门的机器上编译的 其中native库 不一定能适用于每个机器 导致在启动hadoop过程中 出现烦人的警告 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform… using builtin-java classes w…

探索图像检索:从理论到实战的应用

目录 一、引言二、图像检索技术概述图像检索的基本概念图像检索与文本检索的区别特征提取技术相似度计算索引技术 三、图像检索技术代码示例图像特征提取示例相似度计算索引技术 四、图像搜索流程架构数据采集与预处理特征提取相似度计算与排名结果呈现与优化 五、实际应用图像…

探索世界,从一款好用的浏览器开始!

好用的浏览器分享 在这个数字化的时代&#xff0c;浏览器已经成为了我们生活中不可或缺的工具。从浏览新闻、社交媒体到工作学习&#xff0c;我们几乎无时无刻不在与浏览器打交道。那么&#xff0c;如何选择一款好用的浏览器呢&#xff1f;今天&#xff0c;我就来为大家分享几…

git中合并分支时出现了代码冲突怎么办

目录 第一章、Git代码冲突介绍1.1&#xff09;什么是Git代码冲突①git merge命令介绍②代码冲突原因 1.2&#xff09;提示代码冲突的两种情况①本地不同分支的文件有差异时&#xff1a;②本地仓库和git远程仓库的文件有差异时&#xff1a; 1.3&#xff09;解决合并时的代码冲突…