我參考網上的博文自己寫了一個非常短的程式用來學習QFileSystemWatcher,
代碼如下:
mainwindow.cpp檔案:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
fileWatcher = new QFileSystemWatcher(this);
fileWatcher->addPath("C:/Users/Administrator/Desktop/1234/123.txt");
fileWatcher->addPath("/home/owen/test");
connect(fileWatcher, SIGNAL(fileChanged(const QString&)), this, SLOT(m_fileChanged(const QString &)));
connect(fileWatcher,SIGNAL(directoryChanged(const QString&)),this,SLOT(m_directChanged(const QString &)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::m_fileChanged(const QString &path){
qDebug() <<"m_fileChanged" <<path;
}
void MainWindow::m_directChanged(const QString &path){
qDebug() <<"m_directChanged" <<path;
}
mainwindow.h 檔案:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileSystemWatcher>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void m_fileChanged(const QString &path);
void m_directChanged(const QString &path);
private:
Ui::MainWindow *ui;
QFileSystemWatcher *fileWatcher;
};
#endif // MAINWINDOW_H
在Windows 系統上測驗運行正常,當檔案內容改變并保存后會觸發一次fileChanged 信號
但是當我修改
fileWatcher->addPath("C:/Users/Administrator/Desktop/1234/123.txt");
為
fileWatcher->addPath("/home/a123/123.txt");并在Linux(Ubuntu20.04)下測驗的時候發現,修改并保存檔案后會觸發兩次fileChanged 信號,為什么會這樣?怎么修改讓觸發一次fileChanged 信號被?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/278653.html
標籤:Qt
