class MainWindow(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.view = QListView(self) self.view.setModel(CheckableFilesystemModel(self.view)) self.view.activated.connect(self.openIndex) self.setCentralWidget(self.view) open_action = QAction(self.style().standardIcon( QStyle.SP_DialogOpenButton), 'Open', self) open_action.triggered.connect(self.askOpenDirectory) show_action = QAction('Show checked filenames', self) show_action.triggered.connect(self.showCheckedFiles) actions = self.addToolBar('Actions') actions.addAction(open_action) actions.addAction(show_action) self.openDirectory(os.path.expanduser('~')) def askOpenDirectory(self): directory = QFileDialog.getExistingDirectory( self, 'Open directory ...', os.path.expanduser('~')) if directory: self.openDirectory(directory) def openDirectory(self, directory): if os.path.isdir(directory): root = self.view.model().setRootPath(directory) self.view.setRootIndex(root) def openIndex(self, index): model = self.view.model() directory = model.data(index, QFileSystemModel.FilePathRole) self.openDirectory(directory) def showCheckedFiles(self): checked_files = self.view.model().checked_files if checked_files: print('\n'.join(checked_files)) filelist = ''.join('<li>{0}</li>'.format(escape(fn)) for fn in checked_files) QMessageBox.information(self, 'Checked files', '<ul>{0}</ul>'.format(filelist),) else: print('no files checked') QMessageBox.information(self, 'No files checked', 'No files checked')
class FileBrowser(QMainWindow): """Example file browsing widget. Based of the C++ example.""" def __init__(self, parent=None, flags=Qt.Widget): super(FileBrowser, self).__init__(parent, flags) self.gallery = QDocumentGallery(self) self.fileSystemModel = QFileSystemModel(self) self.rootPath = QDesktopServices.storageLocation(QDesktopServices.HomeLocation) self.fileSystemModel.setRootPath(self.rootPath) self.view = QListView() self.view.setModel(self.fileSystemModel) self.view.activated.connect(self.activated) self.setCentralWidget(self.view) self.menuBar().addAction(self.tr("Documents"), self.browseDocuments) self.menuBar().addAction(self.tr("Audio"), self.browseAudio) self.menuBar().addAction(self.tr("Images"), self.browseImages) self.menuBar().addAction(self.tr("Videos"), self.browseVideos) self.browseDocuments() def activated(self, index): fileInfo = self.fileSystemModel.fileInfo(index) if fileInfo.isDir() and fileInfo.fileName() != '.': if fileInfo.fileName() == '..': parent = self.view.rootIndex().parent() fileInfo = self.fileSystemModel.fileInfo(parent) if fileInfo.absoluteFilePath() == self.rootPath: self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(parent) else: self.fileSystemModel.setFilter(QDir.AllEntries | QDir.AllDirs) self.view.setRootIndex(index) self.setWindowTitle(fileInfo.fileName()) else: if fileInfo.fileName() == '.': fileInfo = self.fileSystemModel.fileInfo(self.view.rootIndex()) widget = DocumentPropertiesWidget(fileInfo, self.gallery, self) widget.setWindowFlags(self.window().windowFlags() | Qt.Dialog) widget.setAttribute(Qt.WA_DeleteOnClose) widget.setWindowModality(Qt.WindowModal) widget.show() def browseAudio(self): self.rootPath = QDesktopServices.storageLocation(QDesktopServices.MusicLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Audio")) def browseDocuments(self): self.rootPath = QDesktopServices.storageLocation(QDesktopServices.DocumentsLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Documents")) def browseImages(self): self.rootPath = QDesktopServices.storageLocation(QDesktopServices.PicturesLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Images")) def browseVideos(self): self.rootPath = QDesktopServices.storageLocation(QDesktopServices.MoviesLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Videos"))
class FileBrowser(QMainWindow): """Example file browsing widget. Based of the C++ example.""" def __init__(self, parent=None, flags=Qt.Widget): super(FileBrowser, self).__init__(parent, flags) self.gallery = QDocumentGallery(self) self.fileSystemModel = QFileSystemModel(self) self.rootPath = QDesktopServices.storageLocation( QDesktopServices.HomeLocation) self.fileSystemModel.setRootPath(self.rootPath) self.view = QListView() self.view.setModel(self.fileSystemModel) self.view.activated.connect(self.activated) self.setCentralWidget(self.view) self.menuBar().addAction(self.tr("Documents"), self.browseDocuments) self.menuBar().addAction(self.tr("Audio"), self.browseAudio) self.menuBar().addAction(self.tr("Images"), self.browseImages) self.menuBar().addAction(self.tr("Videos"), self.browseVideos) self.browseDocuments() def activated(self, index): fileInfo = self.fileSystemModel.fileInfo(index) if fileInfo.isDir() and fileInfo.fileName() != '.': if fileInfo.fileName() == '..': parent = self.view.rootIndex().parent() fileInfo = self.fileSystemModel.fileInfo(parent) if fileInfo.absoluteFilePath() == self.rootPath: self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(parent) else: self.fileSystemModel.setFilter(QDir.AllEntries | QDir.AllDirs) self.view.setRootIndex(index) self.setWindowTitle(fileInfo.fileName()) else: if fileInfo.fileName() == '.': fileInfo = self.fileSystemModel.fileInfo(self.view.rootIndex()) widget = DocumentPropertiesWidget(fileInfo, self.gallery, self) widget.setWindowFlags(self.window().windowFlags() | Qt.Dialog) widget.setAttribute(Qt.WA_DeleteOnClose) widget.setWindowModality(Qt.WindowModal) widget.show() def browseAudio(self): self.rootPath = QDesktopServices.storageLocation( QDesktopServices.MusicLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Audio")) def browseDocuments(self): self.rootPath = QDesktopServices.storageLocation( QDesktopServices.DocumentsLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Documents")) def browseImages(self): self.rootPath = QDesktopServices.storageLocation( QDesktopServices.PicturesLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Images")) def browseVideos(self): self.rootPath = QDesktopServices.storageLocation( QDesktopServices.MoviesLocation) self.fileSystemModel.setFilter(QDir.AllEntries | QDir.NoDotAndDotDot | QDir.AllDirs) self.view.setRootIndex(self.fileSystemModel.index(self.rootPath)) self.setWindowTitle(self.tr("Videos"))