您好,登錄后才能下訂單哦!
####Qt實現 實時監控文件夾狀態:文件夾內容增刪、重命名、刪除等
//重建服務器請求監測類
#ifndef QRECONREQUESTLISTENER_H
#define QRECONREQUESTLISTENER_H
#include <QObject>
#include <QFileInfoList>
class QFileSystemWatcher;
class QReconRequestMonitor : public QObject
{
Q_OBJECT
public:
QReconRequestMonitor( const QString& strDirectory );
~QReconRequestMonitor();
signals:
void sigCommitReconRequest( const QString& strFilePath );
protected slots:
void slotDirectoryChanged( const QString& strDirectory );
private:
QStringList GetFileNames( const QFileInfoList& fileInfoList );
private:
QString m_strMonitorDirectory;
QStringList m_strListFileNames;
QFileSystemWatcher* m_pDirectoryWatcher;
};
#endif // QRECONREQUESTLISTENER_H
#include "QReconRequestMonitor.h"
#include <QDir>
#include <QStringList>
#include <QFileSystemWatcher>
QReconRequestMonitor::QReconRequestMonitor( const QString& strDirectory )
{
m_strListFileNames.clear();
m_strListFileNames = QDir( strDirectory ).entryList();
m_strMonitorDirectory = strDirectory;
m_pDirectoryWatcher = new QFileSystemWatcher( this );
m_pDirectoryWatcher->addPath( strDirectory );
connect( m_pDirectoryWatcher, SIGNAL( directoryChanged( const QString& ) ), this, SLOT( slotDirectoryChanged( const QString& ) ) );
}
QReconRequestMonitor::~QReconRequestMonitor()
{
m_strListFileNames.clear();
}
void QReconRequestMonitor::slotDirectoryChanged( const QString& )
{
QStringList strListFileNames; //To save new file names
QFileInfoList fileInfoList = QDir( m_strMonitorDirectory ).entryInfoList();
for( int n = 0; n < fileInfoList.size(); n++ )
{
QFileInfo fileInfo = fileInfoList[ n ];
if( fileInfo.fileName().compare( "." ) == 0 || fileInfo.fileName().compare( ".." ) == 0 )
continue;
if( fileInfo.isDir() )
continue;
QString strFileName = fileInfo.fileName();
if( !m_strListFileNames.contains( strFileName ) )
strListFileNames << strFileName;
}
if( strListFileNames.isEmpty() )
{
m_strListFileNames.clear();
m_strListFileNames = GetFileNames( fileInfoList );
return;
}
for( int n = 0; n < strListFileNames.size(); n++ )
{
//commit recon request
QString strFilePath = m_strMonitorDirectory + "/" + strListFileNames[ n ];
emit sigCommitReconRequest( strFilePath );
}
m_strListFileNames.clear();
m_strListFileNames = GetFileNames( fileInfoList );
return;
}
QStringList QReconRequestMonitor::GetFileNames( const QFileInfoList& fileInfoList )
{
QStringList strLstNames;
int nFileNum = fileInfoList.size();
for( int n = 0; n < nFileNum; n++ )
{
QFileInfo fileInfo = fileInfoList[ n ];
strLstNames << fileInfo.fileName();
}
return strLstNames;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。