溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Qt實(shí)現(xiàn) 實(shí)時(shí)監(jiān)控文件夾狀態(tài)

發(fā)布時(shí)間:2020-06-24 17:38:43 來(lái)源:網(wǎng)絡(luò) 閱讀:3971 作者:006玩命 欄目:編程語(yǔ)言

####Qt實(shí)現(xiàn) 實(shí)時(shí)監(jiān)控文件夾狀態(tài):文件夾內(nèi)容增刪、重命名、刪除等

//重建服務(wù)器請(qǐng)求監(jiān)測(cè)類

#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問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI