当前位置: 首页 > article >正文

Qt 文件目录操作

Qt 文件目录操作

QDir 类提供访问系统目录结构

QDir 类提供对目录结构及其内容的访问。QDir 用于操作路径名、访问有关路径和文件的信息以及操作底层文件系统。它还可以用于访问 Qt 的资源系统。

Qt 使用“/”作为通用目录分隔符,与“/”在 URL 中用作路径分隔符的方式相同。如果您总是使用“/”作为目录分隔符,Qt 将转换您的路径以符合底层操作系统。

QDir 可以使用相对路径或绝对路径指向文件。绝对路径以目录分隔符开头(在 Windows 下,可以选择以驱动器规格开头)。相对文件名以目录名或文件名开头,并指定相对于当前目录的路径。

下面是QDir的一些常用方法:

  1. 构造函数:QDir(const QString &path = QString())
  2. 判断路径是否存在:bool exists() const;
  3. 判断是否是文件:bool isFile() const;
  4. 判断是否是文件夹:bool isDir() const;
  5. 获取当前路径:QString absolutePath() const;
  6. 获取文件名:QString fileName() const;
  7. 获取文件目录:QString dirName() const;
  8. 获取文件大小:qint64 size() const;
  9. 获取文件的创建时间:QDateTime created() const;
  10. 获取文件的最后修改时间:QDateTime lastModified() const;

main函数 main.cpp 文件:

#include "sample7_3QDirFile.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);

	QCoreApplication::setOrganizationName("China University of Petroleum");

	sample7_3QDirFile w;
	w.show();
	return a.exec();
}

sample7_3QDirFile.h 文件

#pragma once

#include <QtWidgets/QDialog>
#include "ui_sample7_3QDirFile.h"

#include   <QFileSystemWatcher>



namespace Ui {
	class sample7_3QDirFile;
}

class sample7_3QDirFile : public QDialog
{
	Q_OBJECT

public:
	sample7_3QDirFile(QWidget *parent = Q_NULLPTR);
	~sample7_3QDirFile();

private:
	Ui::sample7_3QDirFileClass *ui;

public slots:
	void on_directoryChanged(const QString path);
	void on_fileChanged(const QString path);

private:
	QFileSystemWatcher  fileWatcher;
	void showBtnInfo(QObject* btn);

private slots:
    ///--------QCoreApplication类---------------
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void on_pushButton_3_clicked();
    void on_pushButton_4_clicked();
    void on_pushButton_5_clicked();
	void on_pushButton_6_clicked();


	///--------QFile类-文件操作---------------
	void on_pushButton_7_clicked();
	void on_pushButton_8_clicked();
	void on_pushButton_9_clicked();
	void on_pushButton_10_clicked();

	void on_pushButton_11_clicked();
	void on_pushButton_12_clicked();
	void on_pushButton_13_clicked();
	void on_pushButton_14_clicked();


	///--------QFileInfo类 - 文件信息---------------
	void on_pushButton_15_clicked();
	void on_pushButton_16_clicked();
	void on_pushButton_17_clicked();
	void on_pushButton_18_clicked();

	void on_pushButton_19_clicked();
	void on_pushButton_20_clicked();
	void on_pushButton_21_clicked();
	void on_pushButton_22_clicked();

	void on_pushButton_23_clicked();
	void on_pushButton_24_clicked();
	void on_pushButton_25_clicked();
	void on_pushButton_26_clicked();

	void on_pushButton_27_clicked();
	void on_pushButton_28_clicked();
	void on_pushButton_29_clicked();
	void on_pushButton_30_clicked();
	void on_pushButton_31_clicked();
	void on_pushButton_32_clicked();


	///--------QDir类-目录操作---------------
	void on_pushButton_33_clicked();
	void on_pushButton_34_clicked();
	void on_pushButton_35_clicked();
	void on_pushButton_36_clicked();
	void on_pushButton_37_clicked();
	void on_pushButton_38_clicked();

	void on_pushButton_39_clicked();
	void on_pushButton_40_clicked();
	void on_pushButton_41_clicked();
	void on_pushButton_42_clicked();
	void on_pushButton_43_clicked();
	void on_pushButton_44_clicked();

	void on_pushButton_45_clicked();
	void on_pushButton_46_clicked();
	void on_pushButton_47_clicked();
	void on_pushButton_48_clicked();

	void on_pushButton_49_clicked();
	void on_pushButton_50_clicked();
	void on_pushButton_51_clicked();
	void on_pushButton_52_clicked();


	///--------QTemporaryDir和QTemporaryFile---------------
	void on_pushButton_53_clicked();
	void on_pushButton_54_clicked();


	///--------QFileSystemWatcher类---------------
	void on_pushButton_55_clicked();
	void on_pushButton_56_clicked();
	void on_pushButton_57_clicked();
	void on_pushButton_58_clicked();


	//打开文件
	void on_pushButton_59_clicked();
	void on_pushButton_60_clicked();
	void on_pushButton_61_clicked();

};

sample7_3QDirFile.cpp 文件:

#include "sample7_3QDirFile.h"

#include    <QFileDialog>
#include    <QDateTime>
#include    <QTemporaryDir>
#include    <QTemporaryFile>


//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")


sample7_3QDirFile::sample7_3QDirFile(QWidget *parent)
	: QDialog(parent)
{
	ui->setupUi(this);

	this->setWindowFlags(Qt::Window);
}
sample7_3QDirFile::~sample7_3QDirFile()
{
	delete ui;
}

//显示btn的信息
void sample7_3QDirFile::showBtnInfo(QObject *btn)
{ 
	QPushButton *theBtn = static_cast<QPushButton*>(btn);
	ui->plainTextEdit->appendPlainText(theBtn->text());
	ui->plainTextEdit->appendPlainText(theBtn->toolTip() + "\n");
}

//directoryChanged()信号的槽函数
void sample7_3QDirFile::on_directoryChanged(const QString path)
{
	ui->plainTextEdit->appendPlainText(path);
	ui->plainTextEdit->appendPlainText("目录发生了变化\n");
}

//fileChanged()信号的槽函数
void sample7_3QDirFile::on_fileChanged(const QString path)
{
	ui->plainTextEdit->appendPlainText(path);
	ui->plainTextEdit->appendPlainText("文件发生了变化\n");
}


///--------QCoreApplication类---------------
void sample7_3QDirFile::on_pushButton_clicked()
{
	showBtnInfo(sender());
	QString str = QCoreApplication::applicationDirPath();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_2_clicked()
{
	showBtnInfo(sender());
	QString str = QCoreApplication::applicationFilePath();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_3_clicked()
{
	showBtnInfo(sender());
	QString str = QCoreApplication::applicationName();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_4_clicked()
{
	showBtnInfo(sender());
	QStringList strList = QCoreApplication::libraryPaths();
	for (int i = 0; i < strList.count(); i++)
	{
		ui->plainTextEdit->appendPlainText(strList.at(i));
	}

	ui->plainTextEdit->appendPlainText("");
}

void sample7_3QDirFile::on_pushButton_5_clicked()
{
	showBtnInfo(sender());
	QString str = QCoreApplication::organizationName();
	ui->plainTextEdit->appendPlainText(str + "\n");

}

void sample7_3QDirFile::on_pushButton_6_clicked()
{
	QCoreApplication::exit();
}


///--------QFile类-文件操作---------------
void sample7_3QDirFile::on_pushButton_7_clicked()
{
	showBtnInfo(sender());
	QString sous = ui->editFile->text(); //源文件
	QFileInfo   fileInfo(sous);//
	QString newFile = fileInfo.path() + "/" + fileInfo.baseName() + "--副本." + fileInfo.suffix();
	QFile::copy(sous, newFile);
	ui->plainTextEdit->appendPlainText("源文件:" + sous);
	ui->plainTextEdit->appendPlainText("复制为文件:" + newFile + "\n");
}

void sample7_3QDirFile::on_pushButton_8_clicked()
{
	showBtnInfo(sender());
	QString sous = ui->editFile->text(); //源文件
	bool the = QFile::exists(sous);
	if (the)
	{
		ui->plainTextEdit->appendPlainText(+"true \n");
	}
	else
	{
		ui->plainTextEdit->appendPlainText(+"false \n");
	}
}

void sample7_3QDirFile::on_pushButton_9_clicked()
{
	showBtnInfo(sender());
	QString sous = ui->editFile->text(); //源文件
	QFile::remove(sous);
	ui->plainTextEdit->appendPlainText("删除文件:" + sous + "\n");
}
//QFile::rename()
void sample7_3QDirFile::on_pushButton_10_clicked()
{
	showBtnInfo(sender());
	QString sous = ui->editFile->text(); //源文件
	QFileInfo   fileInfo(sous);//源文件信息
	QString newFile = fileInfo.path() + "/" + fileInfo.baseName() + ".XYZ"; //更改文件后缀为".XYZ"
	QFile::rename(sous, newFile); //重命名文件
	ui->plainTextEdit->appendPlainText("源文件:" + sous);
	ui->plainTextEdit->appendPlainText("重命名为:" + newFile + "\n");
}

void sample7_3QDirFile::on_pushButton_11_clicked()
{
	showBtnInfo(sender());
	QString sous = ui->editFile->text(); //源文件
	QFile   file(sous);

	QFileInfo   fileInfo(sous);//
	QString newFile = fileInfo.path() + "/" + fileInfo.baseName() + "--副本." + fileInfo.suffix();

	file.copy(newFile);
	ui->plainTextEdit->appendPlainText("源文件:" + sous);
	ui->plainTextEdit->appendPlainText("复制为文件:" + newFile + "\n");
}

void sample7_3QDirFile::on_pushButton_12_clicked()
{
	showBtnInfo(sender());
	QString sous = ui->editFile->text(); //源文件
	QFile   file(sous);
	bool the = file.exists();
	if (the)
		ui->plainTextEdit->appendPlainText(+"true \n");
	else
		ui->plainTextEdit->appendPlainText(+"false \n");
}

void sample7_3QDirFile::on_pushButton_13_clicked()
{
	showBtnInfo(sender());
	QString sous = ui->editFile->text(); //源文件
	QFile   file(sous);
	file.remove();
	ui->plainTextEdit->appendPlainText("删除文件:" + sous + "\n");
}

//QFile.rename()
void sample7_3QDirFile::on_pushButton_14_clicked()
{
	showBtnInfo(sender());
	QString sous = ui->editFile->text(); //源文件
	QFile   file(sous); //源文件对象
	QFileInfo   fileInfo(sous);//源文件信息
	QString newFile = fileInfo.path() + "/" + fileInfo.baseName() + ".XYZ"; //修改文件后缀为“.XYZ”
	file.rename(newFile); //重命名文件
	ui->plainTextEdit->appendPlainText("源文件:" + sous);
	ui->plainTextEdit->appendPlainText("重命名为:" + newFile + "\n");
}


///--------QFileInfo类 - 文件信息---------------
//QFileInfo::absoluteFilePath()
void sample7_3QDirFile::on_pushButton_15_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QString  str = fileInfo.absoluteFilePath();
	ui->plainTextEdit->appendPlainText(str + "\n");
}
//QFileInfo::absolutePath()
void sample7_3QDirFile::on_pushButton_16_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QString  str = fileInfo.absolutePath();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_17_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QString  str = fileInfo.fileName();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_18_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QString  str = fileInfo.filePath();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

//QFileInfo.size()
void sample7_3QDirFile::on_pushButton_19_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	qint64  dt = fileInfo.size();
	QString  str = QString("%1 Bytes").arg(dt);
	ui->plainTextEdit->appendPlainText(str + "\n");
}

//QFileInfo.path()
void sample7_3QDirFile::on_pushButton_20_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QString  str = fileInfo.path();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

//QFileInfo.basename()
void sample7_3QDirFile::on_pushButton_21_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QString  str = fileInfo.baseName();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_22_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QString  str = fileInfo.completeBaseName();
	ui->plainTextEdit->appendPlainText(str + "\n");

}

void sample7_3QDirFile::on_pushButton_23_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QString  str = fileInfo.suffix();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_24_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QString  str = fileInfo.completeSuffix();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_25_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editDir->text());
	bool  the = fileInfo.isDir();
	if (the)
		ui->plainTextEdit->appendPlainText(+"true \n");
	else
		ui->plainTextEdit->appendPlainText(+"false \n");
}

void sample7_3QDirFile::on_pushButton_26_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	bool  the = fileInfo.isFile();
	if (the)
		ui->plainTextEdit->appendPlainText(+"true \n");
	else
		ui->plainTextEdit->appendPlainText(+"false \n");
}

void sample7_3QDirFile::on_pushButton_27_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	bool  the = fileInfo.isExecutable();
	if (the)
		ui->plainTextEdit->appendPlainText(+"true \n");
	else
		ui->plainTextEdit->appendPlainText(+"false \n");
}

void sample7_3QDirFile::on_pushButton_28_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QDateTime  dt = fileInfo.created();
	QString  str = dt.toString("yyyy-MM-dd hh:mm:ss");
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_29_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QDateTime  dt = fileInfo.lastModified();
	QString  str = dt.toString("yyyy-MM-dd hh:mm:ss");
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_30_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	QDateTime  dt = fileInfo.lastRead();
	QString  str = dt.toString("yyyy-MM-dd hh:mm:ss");
	ui->plainTextEdit->appendPlainText(str + "\n");
}

//静态函数exists()
void sample7_3QDirFile::on_pushButton_31_clicked()
{
	showBtnInfo(sender());
	bool  the = QFileInfo::exists(ui->editFile->text());
	if (the)
		ui->plainTextEdit->appendPlainText(+"true \n");
	else
		ui->plainTextEdit->appendPlainText(+"false \n");

}
void sample7_3QDirFile::on_pushButton_32_clicked()
{
	showBtnInfo(sender());
	QFileInfo  fileInfo(ui->editFile->text());
	bool  the = fileInfo.exists();
	if (the)
		ui->plainTextEdit->appendPlainText(+"true \n");
	else
		ui->plainTextEdit->appendPlainText(+"false \n");
}



///--------QDir类-目录操作---------------
void sample7_3QDirFile::on_pushButton_33_clicked()
{
	showBtnInfo(sender());
	QString  str = QDir::tempPath();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_34_clicked()
{
	showBtnInfo(sender());
	QString  str = QDir::rootPath();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_35_clicked()
{
	showBtnInfo(sender());
	QString  str = QDir::homePath();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_36_clicked()
{
	showBtnInfo(sender());
	QFileInfoList  strList = QDir::drives();
	for (int i = 0; i < strList.count(); i++)
	{
		ui->plainTextEdit->appendPlainText(strList.at(i).path());
	}
	ui->plainTextEdit->appendPlainText("\n");
}

void sample7_3QDirFile::on_pushButton_37_clicked()
{
	showBtnInfo(sender());
	QString  str = QDir::currentPath();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_38_clicked()
{
	showBtnInfo(sender());
	QDir::setCurrent(ui->editDir->text());
	QString  str = QDir::currentPath();
	ui->editDir->setText(str);
	ui->plainTextEdit->appendPlainText("设置当前路径为:" + str + "\n");
}
//-----文件和目录操作------
void sample7_3QDirFile::on_pushButton_39_clicked()
{
	showBtnInfo(sender());
	QString subDir = "subdir1";
	QDir    dir(ui->editDir->text());
	bool   ok = dir.mkdir(subDir);
	if (ok)
	{
		ui->plainTextEdit->appendPlainText("所选目录下成功新建一个目录 " + subDir + "\n");
	}
	else
	{
		ui->plainTextEdit->appendPlainText("创建目录失败\n");
	}
}
void sample7_3QDirFile::on_pushButton_40_clicked()
{
	showBtnInfo(sender());
	QString sous(ui->editDir->text());
	QDir    dir(sous);
	bool   ok = dir.rmdir(sous);
	if (ok)
	{
		ui->plainTextEdit->appendPlainText("成功删除所选目录\n" + sous + "\n");
	}
	else
	{
		ui->plainTextEdit->appendPlainText("删除失败\n");
	}
}
void sample7_3QDirFile::on_pushButton_41_clicked()
{
	showBtnInfo(sender());
	QDir    dir(ui->editDir->text());
	QString sous(ui->editFile->text());
	bool ok = dir.remove(sous);
	if (ok)
	{
		ui->plainTextEdit->appendPlainText("删除文件:" + sous + "\n");
	}
	else
	{
		ui->plainTextEdit->appendPlainText("删除文件失败\n");
	}
}
void sample7_3QDirFile::on_pushButton_42_clicked()
{
	showBtnInfo(sender());

	QDir    dir(ui->editDir->text());
	QString sous = ui->editFile->text(); //源文件
	QFileInfo   fileInfo(sous);//
	QString newFile = fileInfo.path() + "/" + fileInfo.baseName() + ".XYZ";
	dir.rename(sous, newFile);

	ui->plainTextEdit->appendPlainText("源文件:" + sous);
	ui->plainTextEdit->appendPlainText("重命名为:" + newFile + "\n");
}
void sample7_3QDirFile::on_pushButton_43_clicked()
{
	showBtnInfo(sender());
	QString curDir = QDir::currentPath();
	QDir    lastDir(curDir);
	ui->plainTextEdit->appendPlainText("选择目录之前:" + lastDir.absolutePath());

	QString aDir = QFileDialog::getExistingDirectory(this, "选择一个目录", curDir, QFileDialog::ShowDirsOnly);
	if (aDir.isEmpty())
	{
		return;
	}

	ui->editDir->setText(aDir);
	lastDir.setPath(aDir);
	ui->plainTextEdit->appendPlainText("选择目录之后:" + lastDir.absolutePath() + "\n");
}
void sample7_3QDirFile::on_pushButton_44_clicked()
{
	showBtnInfo(sender());
	QDir    dir(ui->editDir->text());
	bool ok = dir.removeRecursively();
	if (ok)
	{
		ui->plainTextEdit->appendPlainText("删除目录及文件成功\n");
	}
	else
	{
		ui->plainTextEdit->appendPlainText("删除目录及文件失败\n");
	}
}

//-----文件或目录信息------
void sample7_3QDirFile::on_pushButton_45_clicked()
{//absoluteFilePath(),
	showBtnInfo(sender());
	QDir    dir(ui->editDir->text());
	QString  str = dir.absoluteFilePath(ui->editFile->text());
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_46_clicked()
{
	showBtnInfo(sender());
	QDir    dir(ui->editDir->text());
	QString  str = dir.absolutePath();
	ui->plainTextEdit->appendPlainText(str + "\n");

}

void sample7_3QDirFile::on_pushButton_47_clicked()
{
	showBtnInfo(sender());
	QDir    dir(ui->editDir->text());
	QString  str = dir.canonicalPath();
	ui->plainTextEdit->appendPlainText(str + "\n");
}

void sample7_3QDirFile::on_pushButton_48_clicked()
{
	showBtnInfo(sender());
	QDir    dir(ui->editDir->text());
	QString  str = dir.filePath(ui->editFile->text());
	ui->plainTextEdit->appendPlainText(str + "\n");

}
void sample7_3QDirFile::on_pushButton_49_clicked()
{
	showBtnInfo(sender());
	QDir    dir(ui->editDir->text());
	bool  the = dir.exists();
	if (the)
		ui->plainTextEdit->appendPlainText("true \n");
	else
		ui->plainTextEdit->appendPlainText("false \n");
}
void sample7_3QDirFile::on_pushButton_50_clicked()
{
	showBtnInfo(sender());
	QDir    dir(ui->editDir->text());
	QString  str = dir.dirName();
	ui->plainTextEdit->appendPlainText(str + "\n");
}
//列出子目录
void sample7_3QDirFile::on_pushButton_51_clicked()
{
	showBtnInfo(sender());

	QDir    dir(ui->editDir->text());
	QStringList strList = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);

	ui->plainTextEdit->appendPlainText("所选目录下的所有目录:");
	for (int i = 0; i < strList.count(); i++)
	{
		ui->plainTextEdit->appendPlainText(strList.at(i));
	}
	ui->plainTextEdit->appendPlainText("\n");

}
//列出所有文件
void sample7_3QDirFile::on_pushButton_52_clicked()
{
	showBtnInfo(sender());
	QDir    dir(ui->editDir->text());
	QStringList strList = dir.entryList(QDir::Files);

	ui->plainTextEdit->appendPlainText("所选目录下的所有文件:");
	for (int i = 0; i < strList.count(); i++)
	{
		ui->plainTextEdit->appendPlainText(strList.at(i));
	}
	ui->plainTextEdit->appendPlainText("\n");
}


///--------QTemporaryDir和QTemporaryFile---------------
//QTemporaryDir
void sample7_3QDirFile::on_pushButton_53_clicked()
{
	showBtnInfo(sender());

	QTemporaryDir    dir;
	dir.setAutoRemove(true);
	ui->plainTextEdit->appendPlainText(dir.path() + "\n");
}
//QTemporaryFile
void sample7_3QDirFile::on_pushButton_54_clicked()
{
	showBtnInfo(sender());

	QTemporaryFile    aFile;
	aFile.setAutoRemove(true);
	aFile.open();
	ui->plainTextEdit->appendPlainText(aFile.fileName() + "\n");
}


///--------QFileSystemWatcher类---------------
//开始监听,addPath()
void sample7_3QDirFile::on_pushButton_55_clicked()
{
	showBtnInfo(sender());
	ui->plainTextEdit->appendPlainText("监听目录:" + ui->editDir->text() + "\n");
	fileWatcher.addPath(ui->editDir->text());//添加监听目录
	fileWatcher.addPath(ui->editFile->text());//添加监听文件

	QObject::connect(&fileWatcher, &QFileSystemWatcher::directoryChanged,
		this, &sample7_3QDirFile::on_directoryChanged); //directoryChanged
	QObject::connect(&fileWatcher, &QFileSystemWatcher::fileChanged,
		this, &sample7_3QDirFile::on_fileChanged); //fileChanged
}
//停止监听,removePath()
void sample7_3QDirFile::on_pushButton_56_clicked()
{
	showBtnInfo(sender());
	ui->plainTextEdit->appendPlainText("停止监听目录:" + ui->editDir->text() + "\n");
	fileWatcher.removePath(ui->editDir->text());//
	fileWatcher.removePath(ui->editFile->text());//
	QObject::disconnect(&fileWatcher, &QFileSystemWatcher::directoryChanged,
		this, &sample7_3QDirFile::on_directoryChanged); //directoryChanged
	QObject::disconnect(&fileWatcher, &QFileSystemWatcher::fileChanged,
		this, &sample7_3QDirFile::on_fileChanged); //fileChanged
}
//显示监听目录,directories()
void sample7_3QDirFile::on_pushButton_57_clicked()
{
	showBtnInfo(sender());
	QStringList strList = fileWatcher.directories();
	for (int i = 0; i < strList.count(); i++)
	{
		ui->plainTextEdit->appendPlainText(strList.at(i));
	}
	ui->plainTextEdit->appendPlainText("");

}
//显示监听文件,files()
void sample7_3QDirFile::on_pushButton_58_clicked()
{
	showBtnInfo(sender());
	QStringList strList = fileWatcher.files();
	for (int i = 0; i < strList.count(); i++)
	{
		ui->plainTextEdit->appendPlainText(strList.at(i));
	}
	ui->plainTextEdit->appendPlainText("");
}



//打开文件
void sample7_3QDirFile::on_pushButton_59_clicked()
{
	QString curDir = QDir::currentPath();
	QString aFile = QFileDialog::getOpenFileName(this, "打开一个文件", curDir, "所有文件(*.*)");
	ui->editFile->setText(aFile);
}

void sample7_3QDirFile::on_pushButton_60_clicked()
{
	QString curDir = QDir::currentPath();
	QString aDir = QFileDialog::getExistingDirectory(this, "选择一个目录", curDir, QFileDialog::ShowDirsOnly);
	ui->editDir->setText(aDir);
}

void sample7_3QDirFile::on_pushButton_61_clicked()
{
	ui->plainTextEdit->clear();
}




http://www.kler.cn/a/380917.html

相关文章:

  • 【YashanDB知识库】jdbc查询st_geometry类型的数据时抛出YAS-00101错误
  • 【XSSed】攻防实战全记录
  • AOP 面向切面编程的实现原理
  • C语言初阶习题【14】数9的个数
  • GESP CCF C++八级编程等级考试认证真题 2024年12月
  • python面试篇-多并发详解(多线程,多进程,协成整理)---一篇搞定
  • Linux 进程终止 进程等待
  • linux同步执行命令脚本 (xcall)
  • ubuntu工具 -- ubuntu服务器临时没有网络,急需联网下载东西怎么办? 使用手机提供网络
  • AI打造超写实虚拟人物:是科技奇迹还是伦理挑战?
  • 【Python爬虫实战】深入 Selenium:从节点信息提取到检测绕过的全攻略
  • 基于C语言实现的TCP客户端
  • 哈希表相关知识
  • 解决wsl重启后debian配置vm.max_map_count不生效问题以及设置docker开机自启
  • Kafka在大数据处理中的作用及其工作原理
  • 20.04Ubuntu配置opencv并使用头文件
  • CSS--综合练习
  • 商业数据库 - oracle -数据字典
  • SQL 语法学习
  • Spring MVC 完整生命周期和异常处理流程图
  • MySQL学习正式篇
  • 浙江深大智能科技有限公司管控平台服务端存在任意文件上传漏洞
  • nginx安装ssl模块教程
  • java-web-day11-登录校验JWT令牌+过滤器
  • C#实现傅里叶变换算法
  • Spring框架和Spring Boot框架都使用注解来简化配置和提高开发效率,但它们之间存在一些区别