qt6windows7/examples/widgets/itemviews/dirview/main.cpp
2023-10-29 23:33:08 +01:00

63 lines
2.2 KiB
C++

// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QApplication>
#include <QFileSystemModel>
#include <QFileIconProvider>
#include <QScreen>
#include <QScroller>
#include <QTreeView>
#include <QCommandLineParser>
#include <QCommandLineOption>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
QCommandLineParser parser;
parser.setApplicationDescription("Qt Dir View Example");
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption dontUseCustomDirectoryIconsOption("c", "Set QFileSystemModel::DontUseCustomDirectoryIcons");
parser.addOption(dontUseCustomDirectoryIconsOption);
QCommandLineOption dontWatchOption("w", "Set QFileSystemModel::DontWatch");
parser.addOption(dontWatchOption);
parser.addPositionalArgument("directory", "The directory to start in.");
parser.process(app);
const QString rootPath = parser.positionalArguments().isEmpty()
? QString() : parser.positionalArguments().first();
QFileSystemModel model;
QFileIconProvider iconProvider;
model.setIconProvider(&iconProvider);
model.setRootPath("");
if (parser.isSet(dontUseCustomDirectoryIconsOption))
model.setOption(QFileSystemModel::DontUseCustomDirectoryIcons);
if (parser.isSet(dontWatchOption))
model.setOption(QFileSystemModel::DontWatchForChanges);
QTreeView tree;
tree.setModel(&model);
if (!rootPath.isEmpty()) {
const QModelIndex rootIndex = model.index(QDir::cleanPath(rootPath));
if (rootIndex.isValid())
tree.setRootIndex(rootIndex);
}
// Demonstrating look and feel features
tree.setAnimated(false);
tree.setIndentation(20);
tree.setSortingEnabled(true);
const QSize availableSize = tree.screen()->availableGeometry().size();
tree.resize(availableSize / 2);
tree.setColumnWidth(0, tree.width() / 3);
// Make it flickable on touchscreens
QScroller::grabGesture(&tree, QScroller::TouchGesture);
tree.setWindowTitle(QObject::tr("Dir View"));
tree.show();
return app.exec();
}