Qt工作笔记-使用QFileSystemWatcher监控文件是否改变
生活随笔
收集整理的這篇文章主要介紹了
Qt工作笔记-使用QFileSystemWatcher监控文件是否改变
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
程序運(yùn)行截圖如下:
代碼如下:
widget.h
main.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }widget.cpp
#include "widget.h" #include <QFileSystemWatcher> #include <QFile> #include <QDebug>Widget::Widget(QWidget *parent): QWidget(parent) {m_fileWatcher=new QFileSystemWatcher;connect(m_fileWatcher,&QFileSystemWatcher::fileChanged,this,&Widget::onFileChanged);loadFile("demo.txt"); }Widget::~Widget() {}void Widget::loadFile(const QString &path) {if(path.isEmpty()||!QFile::exists(path)){return;}m_fileWatcher->addPath(path); }void Widget::onFileChanged(const QString &path) {QFile file(path);if(file.open(QIODevice::ReadOnly|QIODevice::Text)){qDebug()<<file.readAll();} }總結(jié)
以上是生活随笔為你收集整理的Qt工作笔记-使用QFileSystemWatcher监控文件是否改变的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-各种构造函数汇总以及运算符
- 下一篇: 文件包含漏洞实验