Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qquickfolderlistmodel.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
//![code]
6
#
include
"qquickfolderlistmodel_p.h"
7
#
include
"fileinfothread_p.h"
8
#
include
"fileproperty_p.h"
9
#
include
<
QtCore
/
qloggingcategory
.
h
>
10
#
include
<
qqmlcontext
.
h
>
11
#
include
<
qqmlfile
.
h
>
12
13
QT_BEGIN_NAMESPACE
14
15
Q_STATIC_LOGGING_CATEGORY(lcFolderListModel,
"qt.labs.folderlistmodel"
)
16
17
class
QQuickFolderListModelPrivate
18
{
19
Q_DECLARE_PUBLIC(QQuickFolderListModel)
20
21
public
:
22
QQuickFolderListModelPrivate(QQuickFolderListModel *q) : q_ptr(q) { }
23
24
QQuickFolderListModel *q_ptr;
25
QUrl currentDir;
26
QUrl rootDir;
27
FileInfoThread fileInfoThread;
28
QList<FileProperty> data;
29
QHash<
int
, QByteArray> roleNames;
30
QQuickFolderListModel::SortField sortField = QQuickFolderListModel::Name;
31
QStringList nameFilters = { QLatin1String(
"*"
) };
32
QQuickFolderListModel::Status status = QQuickFolderListModel::Null;
33
bool
sortReversed =
false
;
34
bool
showFiles =
true
;
35
bool
showDirs =
true
;
36
bool
showDirsFirst =
false
;
37
bool
showDotAndDotDot =
false
;
38
bool
showDot =
false
;
39
bool
showDotDot =
false
;
40
bool
showOnlyReadable =
false
;
41
bool
showHidden =
false
;
42
bool
caseSensitive =
true
;
43
bool
sortCaseSensitive =
true
;
44
bool
resettingModel =
false
;
45
46
~QQuickFolderListModelPrivate() {}
47
void
init();
48
void
updateSorting();
49
50
void
finishModelReset();
51
52
// private slots
53
void
_q_directoryChanged(
const
QString &directory,
const
QList<FileProperty> &list);
54
void
_q_directoryUpdated(
const
QString &directory,
const
QList<FileProperty> &list,
int
fromIndex,
int
toIndex);
55
void
_q_sortFinished(
const
QList<FileProperty> &list);
56
void
_q_statusChanged(QQuickFolderListModel::Status s);
57
58
static
QString resolvePath(
const
QUrl &path);
59
};
60
61
62
void
QQuickFolderListModelPrivate::init()
63
{
64
Q_Q(QQuickFolderListModel);
65
qRegisterMetaType<QList<FileProperty> >(
"QList<FileProperty>"
);
66
qRegisterMetaType<QQuickFolderListModel::Status>(
"QQuickFolderListModel::Status"
);
67
q->connect(&fileInfoThread, SIGNAL(directoryChanged(QString,QList<FileProperty>)),
68
q, SLOT(_q_directoryChanged(QString,QList<FileProperty>)));
69
q->connect(&fileInfoThread, SIGNAL(directoryUpdated(QString,QList<FileProperty>,
int
,
int
)),
70
q, SLOT(_q_directoryUpdated(QString,QList<FileProperty>,
int
,
int
)));
71
q->connect(&fileInfoThread, SIGNAL(sortFinished(QList<FileProperty>)),
72
q, SLOT(_q_sortFinished(QList<FileProperty>)));
73
q->connect(&fileInfoThread, SIGNAL(statusChanged(QQuickFolderListModel::Status)),
74
q, SLOT(_q_statusChanged(QQuickFolderListModel::Status)));
75
q->connect(q, SIGNAL(rowCountChanged()), q, SIGNAL(countChanged()));
76
}
77
78
79
void
QQuickFolderListModelPrivate::updateSorting()
80
{
81
Q_Q(QQuickFolderListModel);
82
83
QDir::SortFlags flags;
84
85
switch
(sortField) {
86
case
QQuickFolderListModel::Unsorted:
87
flags |= QDir::Unsorted;
88
break
;
89
case
QQuickFolderListModel::Name:
90
flags |= QDir::Name;
91
break
;
92
case
QQuickFolderListModel::Time:
93
flags |= QDir::Time;
94
break
;
95
case
QQuickFolderListModel::Size:
96
flags |= QDir::Size;
97
break
;
98
case
QQuickFolderListModel::Type:
99
flags |= QDir::Type;
100
break
;
101
}
102
103
emit q->layoutAboutToBeChanged();
104
105
if
(sortReversed)
106
flags |= QDir::Reversed;
107
if
(!sortCaseSensitive)
108
flags |= QDir::IgnoreCase;
109
110
fileInfoThread.setSortFlags(flags);
111
}
112
113
void
QQuickFolderListModelPrivate::finishModelReset()
114
{
115
Q_Q(QQuickFolderListModel);
116
const
bool
wasDataEmpty = data.isEmpty();
117
data.clear();
118
qCDebug(lcFolderListModel) <<
"about to emit endResetModel"
;
119
q->endResetModel();
120
if
(!wasDataEmpty)
121
emit q->rowCountChanged();
122
if
(status != QQuickFolderListModel::Null) {
123
status = QQuickFolderListModel::Null;
124
emit q->statusChanged();
125
}
126
resettingModel =
false
;
127
}
128
129
void
QQuickFolderListModelPrivate::_q_directoryChanged(
const
QString &directory,
const
QList<FileProperty> &list)
130
{
131
qCDebug(lcFolderListModel) <<
"_q_directoryChanged called with directory"
<< directory;
132
Q_Q(QQuickFolderListModel);
133
Q_UNUSED(directory);
134
if
(!resettingModel) {
135
resettingModel =
true
;
136
q->beginResetModel();
137
}
138
139
data = list;
140
q->endResetModel();
141
qCDebug(lcFolderListModel) <<
"- endResetModel called"
;
142
emit q->rowCountChanged();
143
emit q->folderChanged();
144
resettingModel =
false
;
145
}
146
147
148
void
QQuickFolderListModelPrivate::_q_directoryUpdated(
const
QString &directory,
const
QList<FileProperty> &list,
int
fromIndex,
int
toIndex)
149
{
150
Q_Q(QQuickFolderListModel);
151
Q_UNUSED(directory);
152
153
QModelIndex parent;
154
if
(data.size() == list.size()) {
155
QModelIndex modelIndexFrom = q->createIndex(fromIndex, 0);
156
QModelIndex modelIndexTo = q->createIndex(toIndex, 0);
157
data = list;
158
emit q->dataChanged(modelIndexFrom, modelIndexTo);
159
}
else
{
160
// File(s) inserted or removed. Since I do not know how many
161
// or where, I need to update the whole list from the first item.
162
// This is a little pessimistic, but optimizing it would require
163
// more information in the signal from FileInfoThread.
164
if
(data.size() > 0) {
165
q->beginRemoveRows(parent, 0, data.size() - 1);
166
q->endRemoveRows();
167
}
168
data = list;
169
if
(list.size() > 0) {
170
if
(toIndex > list.size() - 1)
171
toIndex = list.size() - 1;
172
q->beginInsertRows(parent, 0, data.size() - 1);
173
q->endInsertRows();
174
}
175
emit q->rowCountChanged();
176
}
177
}
178
179
void
QQuickFolderListModelPrivate::_q_sortFinished(
const
QList<FileProperty> &list)
180
{
181
Q_Q(QQuickFolderListModel);
182
qCDebug(lcFolderListModel) <<
"_q_sortFinished called with"
<< list.size() <<
"files"
;
183
184
QModelIndex parent;
185
if
(data.size() > 0) {
186
qCDebug(lcFolderListModel) <<
"- removing all existing rows..."
;
187
q->beginRemoveRows(parent, 0, data.size()-1);
188
data.clear();
189
q->endRemoveRows();
190
qCDebug(lcFolderListModel) <<
"- ...removed all existing rows"
;
191
}
192
193
qCDebug(lcFolderListModel) <<
"- inserting sorted rows..."
;
194
q->beginInsertRows(parent, 0, list.size()-1);
195
data = list;
196
q->endInsertRows();
197
qCDebug(lcFolderListModel) <<
"- ... inserted sorted rows"
;
198
}
199
200
void
QQuickFolderListModelPrivate::_q_statusChanged(QQuickFolderListModel::Status s)
201
{
202
Q_Q(QQuickFolderListModel);
203
204
if
(status != s) {
205
status = s;
206
emit q->statusChanged();
207
}
208
}
209
210
QString QQuickFolderListModelPrivate::resolvePath(
const
QUrl &path)
211
{
212
QString localPath = QQmlFile::urlToLocalFileOrQrc(path);
213
QUrl localUrl = QUrl(localPath);
214
if
(!localUrl.authority().isEmpty())
215
return
localPath;
216
QString fullPath = localUrl.path();
217
if
(localUrl.scheme().size())
218
fullPath = localUrl.scheme() + QLatin1Char(
':'
) + fullPath;
219
return
QDir::cleanPath(fullPath);
220
}
221
222
/*!
223
\qmlmodule Qt.labs.folderlistmodel
224
\title Qt Labs FolderListModel QML Types
225
\ingroup qmlmodules
226
\brief The FolderListModel provides a model of the contents of a file system folder.
227
228
To use this module, import the module with the following line:
229
230
\qml
231
import Qt.labs.folderlistmodel
232
\endqml
233
*/
234
235
/*!
236
\qmltype FolderListModel
237
\inqmlmodule Qt.labs.folderlistmodel
238
//! \nativetype QQuickFolderListModel
239
\ingroup qtquick-models
240
\brief The FolderListModel provides a model of the contents of a file system folder.
241
242
FolderListModel provides access to information about the contents of a folder
243
in the local file system, exposing a list of files to views and other data components.
244
245
\note This type is made available by importing the \c Qt.labs.folderlistmodel module.
246
\e{Elements in the Qt.labs module are not guaranteed to remain compatible
247
in future versions.}
248
249
\note Some features in FolderListModel depend on \l QFileSystemWatcher. If \l QFileSystemWatcher
250
is disabled, the folder set using \c setFolder is not watched for changes, which results in
251
signals typically emitted on directory changes (like directoryUpdated or directoryChanged) not
252
being emitted without manually calling \c setFolder again. For more information, see
253
\l{Qt Configure Options}.
254
255
\qml
256
import Qt.labs.folderlistmodel
257
\endqml
258
259
The \l folder property specifies the folder to access. Information about the
260
files and directories in the folder is supplied via the model's interface.
261
Components access names and paths via the following roles:
262
263
\list
264
\li \c fileName (\c string)
265
\li \c filePath (\c string)
266
\li \c fileUrl (\c url) (since Qt 5.15)
267
\li \c fileBaseName (\c string)
268
\li \c fileSuffix (\c string)
269
\li \c fileSize (\c qlonglong)
270
\li \c fileModified (\c date)
271
\li \c fileAccessed (\c date)
272
\li \c fileIsDir (\c bool)
273
\endlist
274
275
Additionally a file entry can be differentiated from a folder entry via the
276
isFolder() method.
277
278
\section1 Filtering
279
280
Various properties can be set to filter the number of files and directories
281
exposed by the model.
282
283
The \l nameFilters property can be set to contain a list of wildcard filters
284
that are applied to names of files and directories, causing only those that
285
match the filters to be exposed.
286
287
Directories can be included or excluded using the \l showDirs property,
288
navigation directories can also be excluded by setting the \l showDotAndDotDot
289
property to false, hidden files can be included or excluded using the
290
\l showHidden property.
291
292
It is sometimes useful to limit the files and directories exposed to those
293
that the user can access. The \l showOnlyReadable property can be set to
294
enable this feature.
295
296
\section1 Example Usage
297
298
The following example shows a FolderListModel being used to provide a list
299
of QML files in a \l ListView:
300
301
\qml
302
import QtQuick
303
import Qt.labs.folderlistmodel
304
305
ListView {
306
width: 200; height: 400
307
308
FolderListModel {
309
id: folderModel
310
nameFilters: ["*.qml"]
311
}
312
313
Component {
314
id: fileDelegate
315
required property string fileName
316
Text { text: fileName }
317
}
318
319
model: folderModel
320
delegate: fileDelegate
321
}
322
\endqml
323
324
\section1 Path Separators
325
326
Qt uses "/" as a universal directory separator in the same way that "/" is
327
used as a path separator in URLs. If you always use "/" as a directory
328
separator, Qt will translate your paths to conform to the underlying
329
operating system.
330
331
\sa {QML Data Models}
332
*/
333
334
QQuickFolderListModel::QQuickFolderListModel(QObject *parent)
335
: QAbstractListModel(parent), d_ptr(
new
QQuickFolderListModelPrivate(
this
))
336
{
337
Q_D(QQuickFolderListModel);
338
d->roleNames[FileNameRole] =
"fileName"
;
339
d->roleNames[FilePathRole] =
"filePath"
;
340
d->roleNames[FileBaseNameRole] =
"fileBaseName"
;
341
d->roleNames[FileSuffixRole] =
"fileSuffix"
;
342
d->roleNames[FileSizeRole] =
"fileSize"
;
343
d->roleNames[FileLastModifiedRole] =
"fileModified"
;
344
d->roleNames[FileLastReadRole] =
"fileAccessed"
;
345
d->roleNames[FileIsDirRole] =
"fileIsDir"
;
346
d->roleNames[FileUrlRole] =
"fileUrl"
;
347
d->init();
348
}
349
350
QQuickFolderListModel::~QQuickFolderListModel()
351
{
352
}
353
354
QVariant QQuickFolderListModel::data(
const
QModelIndex &index,
int
role)
const
355
{
356
Q_D(
const
QQuickFolderListModel);
357
QVariant rv;
358
359
const
int
row = index.row();
360
if
(row < 0 || row >= d->data.size())
361
return
rv;
362
363
switch
(role)
364
{
365
case
FileNameRole:
366
rv = d->data.at(row).fileName();
367
break
;
368
case
FilePathRole:
369
rv = d->data.at(row).filePath();
370
break
;
371
case
FileBaseNameRole:
372
rv = d->data.at(row).baseName();
373
break
;
374
case
FileSuffixRole:
375
rv = d->data.at(row).suffix();
376
break
;
377
case
FileSizeRole:
378
rv = d->data.at(row).size();
379
break
;
380
case
FileLastModifiedRole:
381
rv = d->data.at(row).lastModified();
382
break
;
383
case
FileLastReadRole:
384
rv = d->data.at(row).lastRead();
385
break
;
386
case
FileIsDirRole:
387
rv = d->data.at(row).isDir();
388
break
;
389
case
FileUrlRole:
390
rv = QUrl::fromLocalFile(d->data.at(row).filePath());
391
break
;
392
default
:
393
break
;
394
}
395
return
rv;
396
}
397
398
QHash<
int
, QByteArray> QQuickFolderListModel::roleNames()
const
399
{
400
Q_D(
const
QQuickFolderListModel);
401
return
d->roleNames;
402
}
403
404
/*!
405
\qmlproperty int FolderListModel::count
406
\readonly
407
408
Returns the number of items in the current folder that match the
409
filter criteria.
410
*/
411
int
QQuickFolderListModel::rowCount(
const
QModelIndex &parent)
const
412
{
413
Q_D(
const
QQuickFolderListModel);
414
Q_UNUSED(parent);
415
return
d->data.size();
416
}
417
418
QModelIndex QQuickFolderListModel::index(
int
row,
int
,
const
QModelIndex &)
const
419
{
420
return
createIndex(row, 0);
421
}
422
423
/*!
424
\qmlproperty url FolderListModel::folder
425
426
The \a folder property holds a URL for the folder that the model
427
currently provides.
428
429
The value must be a \c file: or \c qrc: URL, or a relative URL.
430
431
The default value is the application's working directory at the time
432
when the FolderListModel is first initialized.
433
*/
434
QUrl QQuickFolderListModel::folder()
const
435
{
436
Q_D(
const
QQuickFolderListModel);
437
return
d->currentDir;
438
}
439
440
void
QQuickFolderListModel::setFolder(
const
QUrl &folder)
441
{
442
Q_D(QQuickFolderListModel);
443
444
if
(folder == d->currentDir)
445
return
;
446
447
// It's possible for the folder to be set twice in quick succession,
448
// in which case we could still be waiting for FileInfoThread to finish
449
// getting the list of files from the previously set folder. We should
450
// at least ensure that the begin/end model reset routine is followed
451
// in the correct order, and not e.g. call beginResetModel twice.
452
if
(d->resettingModel)
453
d->finishModelReset();
454
455
d->resettingModel =
true
;
456
457
QString resolvedPath = QQuickFolderListModelPrivate::resolvePath(folder);
458
459
qCDebug(lcFolderListModel) <<
"about to emit beginResetModel since our folder was set to"
<< folder;
460
beginResetModel();
461
462
//Remove the old path for the file system watcher
463
if
(!d->currentDir.isEmpty())
464
d->fileInfoThread.removePath(d->currentDir.path());
465
466
d->currentDir = folder;
467
468
QFileInfo info(resolvedPath);
469
if
(!info.exists() || !info.isDir()) {
470
d->finishModelReset();
471
return
;
472
}
473
474
d->fileInfoThread.setPath(resolvedPath);
475
}
476
477
478
/*!
479
\qmlproperty url FolderListModel::rootFolder
480
481
When this property is set, the given folder will
482
be treated as the root in the file system, so that
483
you can only traverse subfolders within it.
484
*/
485
QUrl QQuickFolderListModel::rootFolder()
const
486
{
487
Q_D(
const
QQuickFolderListModel);
488
return
d->rootDir;
489
}
490
491
void
QQuickFolderListModel::setRootFolder(
const
QUrl &path)
492
{
493
Q_D(QQuickFolderListModel);
494
495
if
(path.isEmpty())
496
return
;
497
498
QString resolvedPath = QQuickFolderListModelPrivate::resolvePath(path);
499
500
QFileInfo info(resolvedPath);
501
if
(!info.exists() || !info.isDir())
502
return
;
503
if
(path != d->rootDir) {
504
d->fileInfoThread.setRootPath(resolvedPath);
505
d->rootDir = path;
506
emit rootFolderChanged();
507
}
508
}
509
510
511
/*!
512
\qmlproperty url FolderListModel::parentFolder
513
\readonly
514
515
Returns the URL of the parent of the current \l folder.
516
*/
517
QUrl QQuickFolderListModel::parentFolder()
const
518
{
519
Q_D(
const
QQuickFolderListModel);
520
521
const
QString localFile = d->currentDir.toLocalFile();
522
if
(localFile.isEmpty())
523
return
QUrl();
524
QDir dir(localFile);
525
if
(dir.isRoot() || !dir.cdUp())
526
return
QUrl();
527
return
QUrl::fromLocalFile(dir.path());
528
}
529
530
/*!
531
\qmlproperty list<string> FolderListModel::nameFilters
532
533
The \a nameFilters property contains a list of file name filters.
534
The filters may include the ? and * wildcards.
535
536
The example below filters on PNG and JPEG files:
537
538
\qml
539
FolderListModel {
540
nameFilters: [ "*.png", "*.jpg" ]
541
}
542
\endqml
543
544
\note Directories are not excluded by filters.
545
*/
546
QStringList QQuickFolderListModel::nameFilters()
const
547
{
548
Q_D(
const
QQuickFolderListModel);
549
return
d->nameFilters;
550
}
551
552
void
QQuickFolderListModel::setNameFilters(
const
QStringList &filters)
553
{
554
Q_D(QQuickFolderListModel);
555
if
(d->nameFilters == filters)
556
return
;
557
d->fileInfoThread.setNameFilters(filters);
558
d->nameFilters = filters;
559
emit nameFilterChanged();
560
}
561
562
void
QQuickFolderListModel::classBegin()
563
{
564
}
565
566
void
QQuickFolderListModel::componentComplete()
567
{
568
Q_D(QQuickFolderListModel);
569
QString localPath = QQmlFile::urlToLocalFileOrQrc(d->currentDir);
570
if
(localPath.isEmpty() || !QDir(localPath).exists())
571
setFolder(QUrl::fromLocalFile(QDir::currentPath()));
572
d->fileInfoThread.start(QThread::LowPriority);
573
}
574
575
/*!
576
\qmlproperty enumeration FolderListModel::sortField
577
578
The \a sortField property contains the field to use for sorting.
579
\c sortField may be one of:
580
581
\value FolderListModel.Unsorted no sorting is applied
582
\value FolderListModel.Name sort by filename (default)
583
\value FolderListModel.Time sort by time modified
584
\value FolderListModel.Size sort by file size
585
\value FolderListModel.Type sort by file type/extension
586
587
\sa sortReversed
588
*/
589
QQuickFolderListModel::SortField QQuickFolderListModel::sortField()
const
590
{
591
Q_D(
const
QQuickFolderListModel);
592
return
d->sortField;
593
}
594
595
void
QQuickFolderListModel::setSortField(SortField field)
596
{
597
Q_D(QQuickFolderListModel);
598
599
if
(field != d->sortField) {
600
d->sortField = field;
601
d->updateSorting();
602
emit sortFieldChanged();
603
}
604
}
605
606
int
QQuickFolderListModel::roleFromString(
const
QString &roleName)
const
607
{
608
Q_D(
const
QQuickFolderListModel);
609
return
d->roleNames.key(roleName.toLatin1(), -1);
610
}
611
612
/*!
613
\qmlproperty bool FolderListModel::sortReversed
614
615
If set to true, reverses the sort order. The default is false.
616
617
\sa sortField
618
*/
619
bool
QQuickFolderListModel::sortReversed()
const
620
{
621
Q_D(
const
QQuickFolderListModel);
622
return
d->sortReversed;
623
}
624
625
void
QQuickFolderListModel::setSortReversed(
bool
rev)
626
{
627
Q_D(QQuickFolderListModel);
628
629
if
(rev != d->sortReversed) {
630
d->sortReversed = rev;
631
d->updateSorting();
632
emit sortReversedChanged();
633
}
634
}
635
636
/*!
637
\qmlmethod bool FolderListModel::isFolder(int index)
638
639
Returns true if the entry \a index is a folder; otherwise
640
returns false.
641
*/
642
bool
QQuickFolderListModel::isFolder(
int
index)
const
643
{
644
if
(index != -1) {
645
QModelIndex idx = createIndex(index, 0);
646
if
(idx.isValid()) {
647
QVariant var = data(idx, FileIsDirRole);
648
if
(var.isValid())
649
return
var.toBool();
650
}
651
}
652
return
false
;
653
}
654
655
/*!
656
\qmlproperty bool FolderListModel::showFiles
657
\since 5.2
658
659
If true, files are included in the model; otherwise only directories
660
are included.
661
662
By default, this property is true.
663
664
\sa showDirs
665
*/
666
bool
QQuickFolderListModel::showFiles()
const
667
{
668
Q_D(
const
QQuickFolderListModel);
669
return
d->showFiles;
670
}
671
672
void
QQuickFolderListModel::setShowFiles(
bool
on)
673
{
674
Q_D(QQuickFolderListModel);
675
676
if
(on != d->showFiles) {
677
d->fileInfoThread.setShowFiles(on);
678
d->showFiles = on;
679
emit showFilesChanged();
680
}
681
}
682
683
/*!
684
\qmlproperty bool FolderListModel::showDirs
685
686
If true, directories are included in the model; otherwise only files
687
are included.
688
689
By default, this property is true.
690
691
Note that the nameFilters are not applied to directories.
692
693
\sa showDotAndDotDot
694
*/
695
bool
QQuickFolderListModel::showDirs()
const
696
{
697
Q_D(
const
QQuickFolderListModel);
698
return
d->showDirs;
699
}
700
701
void
QQuickFolderListModel::setShowDirs(
bool
on)
702
{
703
Q_D(QQuickFolderListModel);
704
705
if
(on != d->showDirs) {
706
d->fileInfoThread.setShowDirs(on);
707
d->showDirs = on;
708
emit showDirsChanged();
709
}
710
}
711
712
/*!
713
\qmlproperty bool FolderListModel::showDirsFirst
714
715
If true, if directories are included in the model they will
716
always be shown first, then the files.
717
718
By default, this property is false.
719
720
*/
721
bool
QQuickFolderListModel::showDirsFirst()
const
722
{
723
Q_D(
const
QQuickFolderListModel);
724
return
d->showDirsFirst;
725
}
726
727
void
QQuickFolderListModel::setShowDirsFirst(
bool
on)
728
{
729
Q_D(QQuickFolderListModel);
730
731
if
(on != d->showDirsFirst) {
732
d->fileInfoThread.setShowDirsFirst(on);
733
d->showDirsFirst = on;
734
emit showDirsFirstChanged();
735
}
736
}
737
738
739
/*!
740
\qmlproperty bool FolderListModel::showDotAndDotDot
741
742
If true, the "." and ".." directories are included in the model; otherwise
743
they are excluded.
744
745
By default, this property is false.
746
747
\sa showDirs, showDot, showDotDot
748
*/
749
bool
QQuickFolderListModel::showDotAndDotDot()
const
750
{
751
Q_D(
const
QQuickFolderListModel);
752
return
d->showDotAndDotDot;
753
}
754
755
void
QQuickFolderListModel::setShowDotAndDotDot(
bool
on)
756
{
757
Q_D(QQuickFolderListModel);
758
759
if
(on != d->showDotAndDotDot) {
760
d->fileInfoThread.setShowDotAndDotDot(on);
761
d->showDotAndDotDot = on;
762
emit showDotAndDotDotChanged();
763
}
764
}
765
766
767
/*!
768
\qmlproperty bool FolderListModel::showDot
769
\since 6.12
770
771
If true, the current "." directory will be included in the model.
772
773
By default, this property is false.
774
775
\sa showDotAndDotDot, showDotDot
776
*/
777
bool
QQuickFolderListModel::showDot()
const
778
{
779
Q_D(
const
QQuickFolderListModel);
780
return
d->showDot;
781
}
782
783
void
QQuickFolderListModel::setShowDot(
bool
on)
784
{
785
Q_D(QQuickFolderListModel);
786
787
if
(on != d->showDot) {
788
d->fileInfoThread.setShowDot(on);
789
d->showDot = on;
790
emit showDotChanged();
791
}
792
}
793
794
795
/*!
796
\qmlproperty bool FolderListModel::showDotDot
797
\since 6.12
798
799
If true, the ".." (parent) directory will be included in the model.
800
801
By default, this property is false.
802
803
\sa showDotAndDotDot, showDot
804
*/
805
bool
QQuickFolderListModel::showDotDot()
const
806
{
807
Q_D(
const
QQuickFolderListModel);
808
return
d->showDotDot;
809
}
810
811
void
QQuickFolderListModel::setShowDotDot(
bool
on)
812
{
813
Q_D(QQuickFolderListModel);
814
815
if
(on != d->showDotDot) {
816
d->fileInfoThread.setShowDotDot(on);
817
d->showDotDot = on;
818
emit showDotDotChanged();
819
}
820
}
821
822
823
/*!
824
\qmlproperty bool FolderListModel::showHidden
825
\since 5.2
826
827
If true, hidden files and directories are included in the model; otherwise
828
they are excluded.
829
830
By default, this property is false.
831
*/
832
bool
QQuickFolderListModel::showHidden()
const
833
{
834
Q_D(
const
QQuickFolderListModel);
835
return
d->showHidden;
836
}
837
838
void
QQuickFolderListModel::setShowHidden(
bool
on)
839
{
840
Q_D(QQuickFolderListModel);
841
842
if
(on != d->showHidden) {
843
d->fileInfoThread.setShowHidden(on);
844
d->showHidden = on;
845
emit showHiddenChanged();
846
}
847
}
848
849
/*!
850
\qmlproperty bool FolderListModel::showOnlyReadable
851
852
If true, only readable files and directories are shown; otherwise all files
853
and directories are shown.
854
855
By default, this property is false.
856
857
\sa showDirs
858
*/
859
bool
QQuickFolderListModel::showOnlyReadable()
const
860
{
861
Q_D(
const
QQuickFolderListModel);
862
return
d->showOnlyReadable;
863
}
864
865
void
QQuickFolderListModel::setShowOnlyReadable(
bool
on)
866
{
867
Q_D(QQuickFolderListModel);
868
869
if
(on != d->showOnlyReadable) {
870
d->fileInfoThread.setShowOnlyReadable(on);
871
d->showOnlyReadable = on;
872
emit showOnlyReadableChanged();
873
}
874
}
875
876
/*!
877
* \qmlproperty bool FolderListModel::caseSensitive
878
* \since 5.7
879
*
880
* Use case sensitive pattern matching.
881
*
882
* By default, this property is true.
883
*
884
*/
885
bool
QQuickFolderListModel::caseSensitive()
const
886
{
887
Q_D(
const
QQuickFolderListModel);
888
return
d->caseSensitive;
889
}
890
891
void
QQuickFolderListModel::setCaseSensitive(
bool
on)
892
{
893
Q_D(QQuickFolderListModel);
894
895
if
(on != d->caseSensitive) {
896
d->fileInfoThread.setCaseSensitive(on);
897
d->caseSensitive = on;
898
emit caseSensitiveChanged();
899
}
900
}
901
902
/*!
903
\qmlproperty enumeration FolderListModel::status
904
\since 5.11
905
\readonly
906
907
This property holds the status of folder reading. It can be one of:
908
909
\value FolderListModel.Null no \a folder has been set
910
\value FolderListModel.Ready the folder has been loaded
911
\value FolderListModel.Loading the folder is currently being loaded
912
913
Use this status to provide an update or respond to the status change in some way.
914
For example, you could:
915
916
\list
917
\li Trigger a state change:
918
\qml
919
State { name: 'loaded'; when: folderModel.status == FolderListModel.Ready }
920
\endqml
921
922
\li Implement an \c onStatusChanged signal handler:
923
\qml
924
FolderListModel {
925
id: folderModel
926
onStatusChanged: if (folderModel.status == FolderListModel.Ready) console.log('Loaded')
927
}
928
\endqml
929
930
\li Bind to the status value:
931
\qml
932
Text { text: folderModel.status == FolderListModel.Ready ? 'Loaded' : 'Not loaded' }
933
\endqml
934
\endlist
935
*/
936
QQuickFolderListModel::Status QQuickFolderListModel::status()
const
937
{
938
Q_D(
const
QQuickFolderListModel);
939
return
d->status;
940
}
941
942
/*!
943
\qmlproperty bool FolderListModel::sortCaseSensitive
944
\since 5.12
945
946
If set to \c true, the sort is case sensitive. This property is \c true by default.
947
*/
948
949
bool
QQuickFolderListModel::sortCaseSensitive()
const
950
{
951
Q_D(
const
QQuickFolderListModel);
952
return
d->sortCaseSensitive;
953
}
954
955
void
QQuickFolderListModel::setSortCaseSensitive(
bool
on)
956
{
957
Q_D(QQuickFolderListModel);
958
959
if
(on != d->sortCaseSensitive) {
960
d->sortCaseSensitive = on;
961
d->updateSorting();
962
emit sortCaseSensitiveChanged();
963
}
964
}
965
966
/*!
967
\qmlmethod var FolderListModel::get(int index, string property)
968
969
Returns the folder \a property for the given \a index. The following properties
970
are available:
971
972
\list
973
\li \c fileName (\c string)
974
\li \c filePath (\c string)
975
\li \c fileUrl (\c url) (since Qt 5.15)
976
\li \c fileBaseName (\c string)
977
\li \c fileSuffix (\c string)
978
\li \c fileSize (\c qlonglong)
979
\li \c fileModified (\c date)
980
\li \c fileAccessed (\c date)
981
\li \c fileIsDir (\c bool)
982
\endlist
983
*/
984
QVariant QQuickFolderListModel::get(
int
idx,
const
QString &property)
const
985
{
986
int
role = roleFromString(property);
987
if
(role >= 0 && idx >= 0)
988
return
data(index(idx), role);
989
else
990
return
QVariant();
991
}
992
993
/*!
994
\qmlmethod int FolderListModel::indexOf(url file)
995
\since 5.6
996
997
Returns the index of the given \a file URL if the model contains it,
998
or -1 if not.
999
*/
1000
int
QQuickFolderListModel::indexOf(
const
QUrl &file)
const
1001
{
1002
Q_D(
const
QQuickFolderListModel);
1003
FileProperty toFind(QFileInfo(QQmlFile::urlToLocalFileOrQrc(file)));
1004
return
d->data.indexOf(toFind);
1005
}
1006
1007
//![code]
1008
QT_END_NAMESPACE
1009
1010
#
include
"moc_qquickfolderlistmodel_p.cpp"
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtdeclarative
src
labs
folderlistmodel
qquickfolderlistmodel.cpp
Generated on
for Qt by
1.18.0