Issue
I have a QDialog that is passed a starting folder, which I expected to be the folder that would be selected and the upper level folders expanded. Everything works as planned, except that the window opens with all of the folders collapsed and only the root icon showing. I must be missing something simple, since this can't be rocket science... 🙄
class FolderSelectDialog(QDialog):
def __init__(self, startingFolderName, parent = ZipRenamer):
super(FolderSelectDialog, self).__init__()
uic.loadUi('rootSelectDialog.ui', self)
fileModel = QFileSystemModel()
fileModel.setFilter(QDir.Dirs | QDir.NoDot | QDir.NoDotDot)
fileModel.setRootPath(startingFolderName)
self.selectedPath = startingFolderName
self.fileSelectTreeView.setModel(fileModel)
self.fileSelectTreeView.resizeColumnToContents(0)
self.fileSelectTreeView.expand(fileModel.index(startingFolderName))
Solution
expand()
only expands the specified index, not its parents. And it shouldn't, because you may want to expand or collapse an index even if any of its parents are collapsed, so that when they are expanded again the previously specified index will use the selected state.
Use setCurrentIndex()
, which will implicitly expand the parent indexes and ensure that the specified index becomes visible.
Also, calling resizeColumnToContents()
at that moment is wrong, because calling it before trying to expand the index will result in resizing the column to the current contents, which, in this case, will be the root path alone, since all its child items are still collapsed.
Use setSectionResizeMode()
instead:
self.fileSelectTreeView.header().setSectionResizeMode(0,
QtWidgets.QHeaderView.ResizeToContents)
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.