1#!/usr/bin/env python 2 3 4############################################################################# 5## 6## Copyright (C) 2017 Riverbank Computing Limited. 7## Copyright (C) 2017 Hans-Peter Jansen <hpj@urpla.net> 8## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 9## All rights reserved. 10## 11## This file is part of the examples of PyQt. 12## 13## $QT_BEGIN_LICENSE:BSD$ 14## You may use this file under the terms of the BSD license as follows: 15## 16## "Redistribution and use in source and binary forms, with or without 17## modification, are permitted provided that the following conditions are 18## met: 19## * Redistributions of source code must retain the above copyright 20## notice, this list of conditions and the following disclaimer. 21## * Redistributions in binary form must reproduce the above copyright 22## notice, this list of conditions and the following disclaimer in 23## the documentation and/or other materials provided with the 24## distribution. 25## * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor 26## the names of its contributors may be used to endorse or promote 27## products derived from this software without specific prior written 28## permission. 29## 30## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 33## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 34## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 36## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 40## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 41## $QT_END_LICENSE$ 42## 43############################################################################# 44 45 46import sys 47 48from PyQt5.QtCore import (QCommandLineOption, QCommandLineParser, 49 QCoreApplication, QDir, QT_VERSION_STR) 50from PyQt5.QtWidgets import (QApplication, QFileIconProvider, QFileSystemModel, 51 QTreeView) 52 53 54app = QApplication(sys.argv) 55 56QCoreApplication.setApplicationVersion(QT_VERSION_STR) 57parser = QCommandLineParser() 58parser.setApplicationDescription("Qt Dir View Example") 59parser.addHelpOption() 60parser.addVersionOption() 61 62dontUseCustomDirectoryIconsOption = QCommandLineOption('c', 63 "Set QFileIconProvider.DontUseCustomDirectoryIcons") 64parser.addOption(dontUseCustomDirectoryIconsOption) 65parser.addPositionalArgument('directory', "The directory to start in.") 66parser.process(app) 67try: 68 rootPath = parser.positionalArguments().pop(0) 69except IndexError: 70 rootPath = None 71 72model = QFileSystemModel() 73model.setRootPath('') 74if parser.isSet(dontUseCustomDirectoryIconsOption): 75 model.iconProvider().setOptions( 76 QFileIconProvider.DontUseCustomDirectoryIcons) 77tree = QTreeView() 78tree.setModel(model) 79if rootPath is not None: 80 rootIndex = model.index(QDir.cleanPath(rootPath)) 81 if rootIndex.isValid(): 82 tree.setRootIndex(rootIndex) 83 84# Demonstrating look and feel features. 85tree.setAnimated(False) 86tree.setIndentation(20) 87tree.setSortingEnabled(True) 88 89availableSize = QApplication.desktop().availableGeometry(tree).size() 90tree.resize(availableSize / 2) 91tree.setColumnWidth(0, tree.width() / 3) 92 93tree.setWindowTitle("Dir View") 94tree.show() 95 96sys.exit(app.exec_()) 97