This is the code i have to display a tree view of a directory called "C:\Myfolder".
import sys
from PyQt4 import QtGui,QtCore
class Myview(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QMainWindow.__init__(self)
model = QtGui.QFileSystemModel()
model.setRootPath('C:\Myfolder')
view = QtGui.QTreeView()
view.setModel(model)
self.setCentralWidget(view)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myview = Myview()
myview.show()
sys.exit(app.exec_())
Even though i set the RootPath to "C:\Myfolder" , the tree view display all the drives and folder.
How can i limit the QFileSystemModel so that TreeView only display items inside "C:\Myfolder" directory?
You would need to add view.setRootIndex(model.index("C:\Myfolder")) according to the QFileSystemModel documentation.
It is extremely important that you write "C:/Myfolder" and NOT "C:\Myfolder". Otherwise, it thinks the directory does not exist and will always show you all drives and all folders.
Related
I have an application where i need to display 2 different TreeViews. One for showing the folders (folderView) and the other will display the Files (fileView) inside the selected folder from the folderView. The following Code works fine but i am having a strange issue:
in the screen shot below, if i click on the bin folder for example, then switch back to VBoxGuestAdd.., the fileView will display the bin folder in the fileView.
p.s.: using an ubuntu 22.04 machine
and here my code:
import sys
from PySide6.QtCore import QDir
from PySide6.QtWidgets import QApplication, QWidget, QHBoxLayout, QTreeView, QFileSystemModel
def folderView_selectionchanged():
current_index = folderView.currentIndex()
selected_folder_path = folderModel.fileInfo(current_index).absoluteFilePath()
fileView.setRootIndex(fileModel.setRootPath(selected_folder_path))
app = QApplication(sys.argv)
window = QWidget()
layout = QHBoxLayout()
folderView = QTreeView()
folderModel = QFileSystemModel()
folderModel.setRootPath("/")
folderModel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs)
folderView.setModel(folderModel)
folderView.selectionModel().selectionChanged.connect(folderView_selectionchanged)
fileView = QTreeView()
fileModel = QFileSystemModel()
fileModel.setRootPath("/")
fileModel.setFilter(QDir.NoDotAndDotDot | QDir.Files)
fileView.setModel(fileModel)
layout.addWidget(folderView)
layout.addWidget(fileView)
window.setLayout(layout)
window.show()
app.exec()
This is a "bug" probably caused by the asynchronous nature of QFileSystemModel, which uses threading to fill the model and delays calls for the model structure updates.
It seems that it's also been already reported as QTBUG-93634, but it has got no attention yet.
A possible workaround is to "reset" the filter and set it again:
def folderView_selectionchanged():
current_index = folderView.currentIndex()
selected_folder_path = folderModel.fileInfo(current_index).absoluteFilePath()
fileView.setRootIndex(fileModel.setRootPath(selected_folder_path))
fileModel.setFilter(QDir.AllDirs)
fileModel.setFilter(QDir.NoDotAndDotDot | QDir.Files)
But the above might not work for big/slow file systems. The only possible solution I can think of is to use a QSortFilterProxyModel and override the filterAcceptsRow() function. It will not be as fast as the basic model, but it will work as expected.
Note that:
filterAcceptsRow() is always checked against the model hierarchy, so always allowing the filter to pass anything outside the filtered directory is mandatory, otherwise it will be filtered out (if the parent directory is filtered out, there is no child to show);
since setRootPath() invalidates the layout and checks the filters again, we must clear the validation until the new root path is set, and then restore it and reset the filters again; this is done by temporarily replacing the actual filtering function with a dummy one that always returns True;
class FileProxy(QSortFilterProxyModel):
validParent = None
def __init__(self):
super().__init__()
self.fsModel = QFileSystemModel()
self.setSourceModel(self.fsModel)
def filterAcceptsRow(self, row, parent):
return (
self.validParent != parent
or not self.fsModel.isDir(
self.fsModel.index(row, 0, parent))
)
def setRootPath(self, path):
func = self.filterAcceptsRow
self.filterAcceptsRow = lambda *args: True
self.validParent = self.fsModel.setRootPath(path)
self.filterAcceptsRow = func
self.invalidateFilter()
return self.mapFromSource(self.validParent)
I'm trying to add items to two combo boxes.
The code below runs with no errors, I see the list I'm trying to add and "fin" is printed to the terminal, but the combo boxes are showing up empty.
from PyQt5.QtWidgets import QMainWindow
from PyQt5 import QtWidgets
# import GUI from designer file
from main_menu import Ui_main_menu
# import other functions
from add_functions import ChangeLists
class Main(QMainWindow, Ui_main_menu):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
self.init_lists()
def init_lists(self):
# Team List
team_list_file = open(r'C:\NHLdb_pyqt\files\NHLteams.txt', 'r')
team_list = team_list_file.read().splitlines()
team_list_file.close()
print("team list: ", team_list)
# Initial Player List
player_list_init = "Please Select a Team"
# Populate combo box lists
self.team_select_combobox.addItems(team_list)
self.player_select_combobox.addItem(player_list_init)
# connect combo box to function that will change player list based on team list selection
# self.team_select_combobox.currentTextChanged.connect(ChangeLists.team_changed)
print("fin")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_menu = QtWidgets.QDialog()
ui = Main()
ui.setupUi(main_menu)
# main_menu = Main()
main_menu.show()
sys.exit(app.exec_())
You're using two methods of loading the ui at the same time, the multiple inheritance approach and the "direct approach", and you're actually showing the main_menu instance of QDialog (which doesn't have any init_lists function).
The result is that even if the init_lists is called, it's "shown" (actually, not) in the wrong window, since you're showing the main_menu instance.
Clearly, you should not use both of them, as the first is enough (and usually the most used/suggested), and then show the correct instance object:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_menu = Main()
main_menu.show()
sys.exit(app.exec_())
Do note that there's something else that is wrong with your implementation: you're inheriting from QMainWindow in the Main class, but later you're trying to set up the ui using a QDialog.
Only the base class created in Designer should be used (it doesn't matter what approach you use to load the ui). I can assume that the ui was created using a QDialog (otherwise an exception would have occurred, as a QMainWindow ui would try to use setCentralWidget() which is a function that doesn't exist for QDialog).
So, you either create a new main window in Designer and copy your existing layout in that (if you need the features of a QMainWindow, such as a menu bar, a status bar, dock widgets or toolbars), or you correctly use the QDialog class in the constructor:
class Main(QDialog, Ui_main_menu):
# ...
I have created a simple test program in Qt Designer that allows you to select a folder and display its contents on a window. It looks like this:
I have successfully converted the .ui file to .py without fail. Next, here is my code to run the program, aptly named main.py:
from PyQt4 import QtGui
import sys
import design
import os
class ExampleApp(QtGui.QMainWindow, design.Ui_MainWindow):
def _init_(self):
super(self._class_, self)._init_()
self.setupUI(self)
self.btnBrowse.clicked.connect(self.browse_folder)
def browse_folder(self):
self.listWidget.clear()
directory = QtGui.QFileDialog.getExistingDirectory(self,"Pick a Folder")
if directory:
for file_name in os.listdir(directory):
self.listWidget.addItem(file_name)
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
In my command prompt, I run the following code:
python main.py
It proceeds to load for a second or two, and then I get this:
Is there something that I am doing wrong? Why isn't my program showing up the way it should be? Any help is appreciated!
These lines are wrong:
def _init_(self):
super(self._class_, self)._init_()
Instead you want something like:
def __init__(self, parent=None):
super(ExampleApp, self).__init__(parent)
Note the double underscores, the different super argument, and passing parent to the super class. I can't test this right now, but it should be much closer to working.
By naming your __init__ method incorrectly it never would've been called. That explains why you get a window but not the one you designed.
I'm trying to implement a folder viewer to view the structure of a specific path. and this folder view should look like a the tree widget in PyQT , i know that the file dialog can help , but i need to have it inside my main window.
i tried to implement this using QTreeWidget and i used a recursed function to loop inside folders, but this is too slow. since it needs to recurse around huge number of folders.
is this the right way to do it? or there is a ready qt solution for this problem.
Check the figure below.
for PyQt5 I did this function :
def load_project_structure(startpath, tree):
"""
Load Project structure tree
:param startpath:
:param tree:
:return:
"""
import os
from PyQt5.QtWidgets import QTreeWidgetItem
from PyQt5.QtGui import QIcon
for element in os.listdir(startpath):
path_info = startpath + "/" + element
parent_itm = QTreeWidgetItem(tree, [os.path.basename(element)])
if os.path.isdir(path_info):
load_project_structure(path_info, parent_itm)
parent_itm.setIcon(0, QIcon('assets/folder.ico'))
else:
parent_itm.setIcon(0, QIcon('assets/file.ico'))
then i call it like this :
load_project_structure("/your/path/here",projectTreeWidget)
and i have this result :
Use models and views.
"""An example of how to use models and views in PyQt4.
Model/view documentation can be found at
http://doc.qt.nokia.com/latest/model-view-programming.html.
"""
import sys
from PyQt4.QtGui import (QApplication, QColumnView, QFileSystemModel,
QSplitter, QTreeView)
from PyQt4.QtCore import QDir, Qt
if __name__ == '__main__':
app = QApplication(sys.argv)
# Splitter to show 2 views in same widget easily.
splitter = QSplitter()
# The model.
model = QFileSystemModel()
# You can setRootPath to any path.
model.setRootPath(QDir.rootPath())
# List of views.
views = []
for ViewType in (QColumnView, QTreeView):
# Create the view in the splitter.
view = ViewType(splitter)
# Set the model of the view.
view.setModel(model)
# Set the root index of the view as the user's home directory.
view.setRootIndex(model.index(QDir.homePath()))
# Show the splitter.
splitter.show()
# Maximize the splitter.
splitter.setWindowState(Qt.WindowMaximized)
# Start the main loop.
sys.exit(app.exec_())
EDIT2: model.hasChildren(parentIndex) returns True, but model.rowCount(parentIndex) returns 0. Is QFileSystemModel just fubar in PyQt?
EDIT: With a bit of adaptation this all works exactly as it should if I use QDirModel. This is deprecated, but maybe QFileSystemModel hasn't been fully implemented in PyQt?
I'm learning the Qt Model/View architecture at the moment, and I've found something that doesn't work as I'd expect it to. I've got the following code (adapted from Qt Model Classes):
from PyQt4 import QtCore, QtGui
model = QtGui.QFileSystemModel()
parentIndex = model.index(QtCore.QDir.currentPath())
print model.isDir(parentIndex) #prints True
print model.data(parentIndex).toString() #prints name of current directory
rows = model.rowCount(parentIndex)
print rows #prints 0 (even though the current directory has directory and file children)
The question:
Is this a problem with PyQt, have I just done something wrong, or am I completely misunderstanding QFileSystemModel? According to the documentation, model.rowCount(parentIndex) should return the number of children in the current directory. (I'm running this under Ubuntu with Python 2.6)
The QFileSystemModel docs say that it needs an instance of a Gui application, so I've also placed the above code in a QWidget as follows, but with the same result:
import sys
from PyQt4 import QtCore, QtGui
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
model = QtGui.QFileSystemModel()
parentIndex = model.index(QtCore.QDir.currentPath())
print model.isDir(parentIndex)
print model.data(parentIndex).toString()
rows = model.rowCount(parentIndex)
print rows
def main():
app = QtGui.QApplication(sys.argv)
widget = Widget()
widget.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I've solved it.
The reason to use QFileSystemModel as opposed to QDirModel is because QFileSystemModel loads the data from the filesystem in a separate thread. The problem with that is that if you try to print the number of children just after it's been constructed is that it won't have loaded the children yet. The way to fix the above code is to add the following:
self.timer = QtCore.QTimer(self)
self.timer.singleShot(1, self.printRowCount)
to the end of the constructor, and add a printRowCount method which will print the correct number of children. Phew.
Since you've already figured it out, just a couple of extra thoughts on what was going on with your model: QFileSystemModel::rowCount returns rows from the visibleChildren collection; I guess you're correctly identified the problem: at the time when you're checking row count it was not yet populated. I've changed your example without using timers; pls, check if it works for you:
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.model = QtGui.QFileSystemModel()
self.model.setRootPath(QtCore.QDir.currentPath())
def checkParent(self):
parentIndex = self.model.index(QtCore.QDir.currentPath())
print self.model.isDir(parentIndex)
print self.model.data(parentIndex).toString()
rows = self.model.rowCount(parentIndex)
print "row count:", rows
def main():
app = QtGui.QApplication(sys.argv)
widget = Widget()
widget.show()
app.processEvents(QtCore.QEventLoop.AllEvents)
widget.checkParent()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I believe your code should work correctly on any UI event after widget constructed is shown on the screen
regards