Qt5開發及實例學習之監聽目錄

#include "watcher.h"
#include <QVBoxLayout>
#include <QDir>
#include <QMessageBox>
#include <QApplication>

Watcher::Watcher(QWidget *parent)
    : QWidget(parent)
{
    QStringList args=qApp->arguments();
    QString path;
    //讀取命令行指定的目錄做爲監聽目錄。
    if(args.count()>1)
    {
        path=args[1];
    }
    else  //獲取沒有指定,監聽當前目錄
    {
        path=QDir::currentPath();
    }

    pathLabel = new QLabel;
    pathLabel->setText(tr("監視的目錄:")+path);
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(pathLabel);

    fsWatcher.addPath(path);
    connect(&fsWatcher,SIGNAL(directoryChanged(QString)),this,SLOT(directoryChanged(QString)));
}

Watcher::~Watcher()
{
    
}

void Watcher::directoryChanged(QString path)
{
    QMessageBox::information(NULL,tr("目錄發生變化"),path);
}

相關文章
相關標籤/搜索