温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Qt实现 实时监控文件夹状态

发布时间:2020-06-24 17:38:43 来源:网络 阅读:3976 作者:006玩命 栏目:编程语言

####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;
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI