快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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

####Qt实现 实时监控文件夹状态:文件夹内容增删、重命名、删除等

创新互联从2013年开始,是专业互联网技术服务公司,拥有项目成都网站设计、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元榆阳做网站,已为上家服务,为榆阳各地企业和个人服务,联系电话:028-86922220

//重建服务器请求监测类

#ifndef QRECONREQUESTLISTENER_H
#define QRECONREQUESTLISTENER_H

#include 
#include 

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 
#include 
#include 

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;
}

当前文章:Qt实现实时监控文件夹状态
文章网址:http://6mz.cn/article/gphsds.html

其他资讯