Home > Mobile >  Questions about QFileSystemWatcher in QT
Questions about QFileSystemWatcher in QT

Time:04-22

I refer to online blog I wrote a very short program is used to study QFileSystemWatcher,
The code is as follows:

Mainwindow. CPP file:

# include "mainwindow. H"
# include "ui_mainwindow. H"
#include
MainWindow: : MainWindow (QWidget * parent)
: QMainWindow (parent)
, the UI (new UI: : MainWindow)
{
The UI - & gt; SetupUi (this);
FileWatcher=new QFileSystemWatcher (this);

FileWatcher - & gt; AddPath (" C:/Users/Administrator/Desktop/1234/123. TXT ");
FileWatcher - & gt; AddPath ("/home/Owen/test ");

The connect (fileWatcher, SIGNAL (fileChanged (const QString&) ), this SLOT (m_fileChanged (const QString & amp;) ));
The connect (fileWatcher, SIGNAL (directoryChanged (const QString&) ), this SLOT (m_directChanged (const QString & amp;) ));
}

MainWindow: : ~ MainWindow ()
{
Delete the UI;
}
Void MainWindow: : m_fileChanged (const QString & amp; The path) {
QDebug () & lt; <" M_fileChanged "& lt;
}
Void MainWindow: : m_directChanged (const QString & amp; The path) {
QDebug () & lt; <" M_directChanged "& lt; }




Mainwindow. H file:

# # ifndef MAINWINDOW_H
# define MAINWINDOW_H

#include
#include
QT_BEGIN_NAMESPACE
The namespace Ui {class MainWindow. }
QT_END_NAMESPACE

The class MainWindow: public QMainWindow
{
Q_OBJECT

Public:
MainWindow (QWidget * parent=nullptr);
~ MainWindow ();

Public slots:
Void m_fileChanged (const QString & amp; The path);
Void m_directChanged (const QString & amp; The path);

Private:
Ui: : MainWindow * Ui;
FileWatcher QFileSystemWatcher *;

};
# endif//MAINWINDOW_H

A test run on Windows system is normal, after the file content change and save the triggers a fileChanged signal


But when I modify
FileWatcher - & gt; AddPath (" C:/Users/Administrator/Desktop/1234/123. TXT ");

FileWatcher - & gt; AddPath ("/home/a123/123. TXT "); And under Linux (Ubuntu20.04) test, found that modify and save the file after the trigger twice fileChanged signal, why? How to modify to trigger a fileChanged signal is?
  •  Tags:  
  • Qt
  • Related