Qt利用QFileSystemWatcher对文件或目录监听fileChanged
相关资料:
https://blog.csdn.net/liang19890820/article/details/51849252
.pro
1 QT += core gui 2 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 5 CONFIG += c++11 6 7 # The following define makes your compiler emit warnings if you use 8 # any Qt feature that has been marked deprecated (the exact warnings 9 # depend on your compiler). Please consult the documentation of the 10 # deprecated API in order to know how to port your code away from it. 11 DEFINES += QT_DEPRECATED_WARNINGS 12 13 # You can also make your code fail to compile if it uses deprecated APIs. 14 # In order to do so, uncomment the following line. 15 # You can also select to disable deprecated APIs only up to a certain version of Qt. 16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 18 SOURCES += \ 19 main.cpp \ 20 mainwindow.cpp 21 22 HEADERS += \ 23 mainwindow.h 24 25 FORMS += \ 26 mainwindow.ui 27 28 # Default rules for deployment. 29 qnx: target.path = /tmp/$${TARGET}/bin 30 else: unix:!android: target.path = /opt/$${TARGET}/bin 31 !isEmpty(target.path): INSTALLS += target
main.cpp
1 #include "mainwindow.h" 2 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 6 #include <QFileSystemWatcher> 7 #include <QDebug> 8 #include <QFile> 9 #include <QStringList> 10 #include <QString> 11 12 13 QT_BEGIN_NAMESPACE 14 namespace Ui { class MainWindow; } 15 QT_END_NAMESPACE 16 17 class MainWindow : public QMainWindow 18 { 19 Q_OBJECT 20 21 public: 22 MainWindow(QWidget *parent = nullptr); 23 ~MainWindow(); 24 25 void SaveCameraConfig(QStringList strliConfig); 26 27 private slots: 28 void on_fileChanged(const QString &path); 29 void on_directoryChanged(const QString &path); 30 31 void on_pushButton_clicked(); 32 33 private: 34 Ui::MainWindow *ui; 35 36 QFileSystemWatcher *m_FileWatcher; 37 QStringList m_listOfConfig; 38 }; 39 #endif // MAINWINDOW_H
mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 10 m_FileWatcher = new QFileSystemWatcher(); 11 m_FileWatcher->addPath("d:/"); 12 m_FileWatcher->addPath("d:/log.txt"); 13 14 connect(m_FileWatcher, &QFileSystemWatcher::fileChanged, this, &MainWindow::on_fileChanged); 15 connect(m_FileWatcher, &QFileSystemWatcher::directoryChanged, this, &MainWindow::on_directoryChanged); 16 } 17 18 MainWindow::~MainWindow() 19 { 20 delete ui; 21 } 22 23 void MainWindow::SaveCameraConfig(QStringList strliConfig) 24 { 25 QStringList strList; 26 QString strFileName = "d:/log.txt"; 27 QFile file(strFileName); 28 if (file.open(QFile::WriteOnly)) { 29 for (int i = 0; i < strliConfig.size(); i++) { 30 QString str = strliConfig[i] + "\r\n"; 31 file.write(str.toStdString().c_str()); 32 } 33 file.close(); 34 } 35 } 36 37 void MainWindow::on_directoryChanged(const QString &path) 38 { 39 qDebug() << QStringLiteral("目录发生改变--") << path; 40 } 41 42 void MainWindow::on_fileChanged(const QString &path) 43 { 44 qDebug() << QStringLiteral("file发生改变--") << path; 45 } 46 47 void MainWindow::on_pushButton_clicked() 48 { 49 int num = m_listOfConfig.size(); 50 m_listOfConfig.append(QString::number(num)); 51 SaveCameraConfig(m_listOfConfig); 52 }
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我