The following code runs (after importing necessary libraries):
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('MainWindow')
self.layout = QHBoxLayout()
self.file_system_widget = FileBrowser()
self.layout.addWidget(self.file_system_widget)
widget = QWidget()
widget.setLayout(self.layout)
self.setCentralWidget(widget)
class FileBrowser(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QVBoxLayout()
self.model = QFileSystemModel()
self.model.setRootPath(self.model.myComputer())
self.model.setNameFilters(['*.*'])
self.model.setNameFilterDisables(1)
self.tree = QTreeView()
self.tree.setModel(self.model)
self.tree.setAnimated(False)
self.tree.setSortingEnabled(True)
layout.addWidget(self.tree)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
try:
os.mkdir('Imports')
except:
pass
main = MainWindow()
main.show()
app.exec()
It gives the following result on my C drive:
(Some of these files are provided here https://drive.google.com/drive/folders/1ejY0CjfEwS6SGS2qe_uRX2JvlruMKvPX).
My objective is to modify the line self.model.setNameFilters(['*.*'])
such that, in the tree view, it only shows files with dcm
extension and also files without extension. That is, the part I draw red gets removed.
How do I achieve such a goal? For keeping dcm
files, I can write lines like self.model.setNameFilters(['*.dcm'])
to keep them and remove the others. But I am not sure how to deal with files without extension or how to deal with the two requirements at the same time .