简写一点qt的文件操作
cpp代码:
#include "widget.h"
#include <QDialog>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <qDebug>
#include <QDir>
// #include "main.moc"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
// dialog_ = new QDialog(this);
// dialog_->setFixedSize(320,240);
// dialog_->show();
// dialog();
// 添加按钮到布局
QPushButton *button = new QPushButton("关闭",this);
// layout->addWidget(button);
// 连接按钮的点击事件到对话框的关闭槽
QObject::connect(button, &QPushButton::clicked, this, &Widget::fileSolt);
}
Widget::~Widget() {}
void Widget::fileSolt()
{
qDebug() << " >>>>>>>>>>>>>>>>>>>>>>>> ";
QString path = "C:\\Users\\1\\Desktop\\test"; // 替换为你想要检查的路径
QString folderName = "FileEnv"; // 替换为你想要检查的文件夹名称
checkOrCreateFolder(path, folderName);
}
void Widget::checkOrCreateFolder(const QString &path, const QString &folderName)
{
QDir dir(path);
// 检查文件夹是否存在
if (!dir.exists(folderName)) {
// 创建文件夹
if (dir.mkdir(folderName)) {
qDebug() << "文件夹已创建:" << dir.absoluteFilePath(folderName);
createFile(path + "\\" + folderName);
} else {
qWarning() << "无法创建文件夹:" << dir.absoluteFilePath(folderName);
}
} else {
qDebug() << "文件夹已存在:" << dir.absoluteFilePath(folderName);
// createFile(path + "\\" + folderName);
listFilesInDirectory(path + "\\" + folderName);
}
}
// 创建文件
void Widget::createFile(const QString &path) {
// 拼接文件路径
QString filePath = QDir(path).filePath("example.lst");
// 创建文件
QFile file(filePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "文件已创建:" << filePath;
QTextStream out(&file);
out << "这是一个示例文件\n";
file.close();
} else {
qWarning() << "无法创建文件:" << filePath;
}
}
// 遍历文件夹下的所有文件
void Widget::listFilesInDirectory(const QString &path)
{
QDir directory(path);
if (!directory.exists()) {
qDebug() << "目录不存在:" << path;
return;
}
// QStringList files = directory.entryList(QDir::Files);
// 指定类型为。lst的文件
QStringList files = directory.entryList(QStringList() << "*.lst", QDir::Files);
qDebug() << "目录中的文件个数为:"<< files.size();
foreach(const QString &file, files) {
QFile fileRead(path + "/" +file);
if (!fileRead.open(QIODevice::ReadOnly)) {
qDebug() << "无法打开文件:" << file;
return;
}
QTextStream in(&fileRead);
while (!in.atEnd()) {
QString line = in.readLine();
qDebug() << line;
}
fileRead.close();
qDebug() << file;
}
}
// 多线程读取
// void threadRead()
// {
// QString filePath = "/path/to/your/file"; // 替换为你想要读取的文件路径
// FileReader fileReader(filePath);
// QThread thread;
// fileReader.moveToThread(&thread);
// // 连接信号和槽
// QObject::connect(&thread, &QThread::started, &fileReader, &FileReader::readFile);
// QObject::connect(&fileReader, &FileReader::lineRead, [](const QString &line) {
// qDebug() << line;
// });
// QObject::connect(&fileReader, &FileReader::error, [](const QString &errorMessage) {
// qDebug() << "错误:" << errorMessage;
// });
// QObject::connect(&fileReader, &FileReader::finished, &thread, &QThread::quit);
// QObject::connect(&thread, &QThread::finished, &fileReader, &QObject::deleteLater);
// thread.start();
// }
// /*
// *
// * 在Qt中,#include "main.moc"语句的作用是告诉编译器在当前文件中包含对应的Qt元对象编译器(moc)生成的元对象代码。
// 当我们使用了Qt元对象系统(比如在类中声明了Q_OBJECT宏)时,编译器需要生成额外的代码来支持信号和槽、属性等元对象特性。这些额外的代码由Qt的元对象编译器(moc)生成,并被存储在名为moc_*.cpp的文件中。然后,这些生成的文件会被编译器编译成目标可执行文件。
// 当我们在一个源文件中使用了Qt元对象系统,但是这个源文件不是一个头文件,也没有被Qt的构建系统(比如qmake)自动处理时,我们就需要手动包含对应的moc生成的代码文件,以确保编译器能够正确地编译程序。因此,需要在这个文件的末尾包含#include "main.moc"来包含与当前源文件相关的moc生成的代码。
// 总之,#include "main.moc"的作用是确保当前源文件包含了对应的Qt元对象编译器生成的元对象代码,以支持使用了Qt元对象系统的功能
// * /
.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDialog>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void dialog();
void fileSolt();
// 检查文件路径是否存在
void checkOrCreateFolder(const QString &path, const QString &folderName);
// 创建文件
void createFile(const QString &path);
// 遍历文件夹下的所有文件
void listFilesInDirectory(const QString &path);
QDialog *widget_;
QDialog *dialog_;
};
#endif // WIDGET_H