-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkfileinfos.cpp
60 lines (50 loc) · 1.54 KB
/
kfileinfos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "kfileinfos.h"
#include <QApplication>
#include <QDir>
#include <QStandardPaths>
#include <QQueue>
KFileInfos *KFileInfos::s_fileInfos = nullptr;
KFileInfos::KFileInfos(QObject *parent) :
QObject{parent}
{
updateFileInfos();
m_timer.setInterval(1000);
connect(&m_timer, &QTimer::timeout, this, &KFileInfos::updateFileInfos, Qt::QueuedConnection);
m_timer.start();
}
KFileInfos::~KFileInfos()
{
m_timer.stop();
}
const KFileInfos *KFileInfos::instance()
{
if (!s_fileInfos)
s_fileInfos = new KFileInfos(qApp); // 设置parent为qApp,加入Qt对象生命周期管理
return s_fileInfos;
}
const QFileInfoList &KFileInfos::fileInfoList() const
{
return m_fileInfoList;
}
void KFileInfos::updateFileInfos()
{
QDir sourceDir = QDir(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
QFileInfoList fileInfos;
QFileInfoList totalFileInfos;
QQueue<QDir> queue;
queue.push_back(sourceDir);
// 广度优先搜索遍历全部文件
while (!queue.empty())
{
sourceDir = queue.dequeue();
fileInfos = sourceDir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot | QDir::Dirs);
totalFileInfos.append(fileInfos);
foreach (const QFileInfo &fileInfo, fileInfos)
if (fileInfo.isDir())
queue.push_back(QDir(fileInfo.absoluteFilePath()));
}
if (totalFileInfos == m_fileInfoList)
return;
m_fileInfoList = totalFileInfos;
emit filesChanged();
}