Cody_Jackson | 2021-03-11 20:16:42 UTC | #1
I want to take multiple selections from a treeView of a file system and process them sequentially, like a batch job. If there's a better way than what I discuss below, I would greatly appreciate it.
I have the following code:
def add_item(self):
"""Add Project Explorer selected asset to Working Directory"""
indexes = self.project.treeView.selectedIndexes()
for index in indexes:
self.console_output.on_update_text(index) # Just to verify data type
self.project.build_project(index)
I know that each index is a Qt index object because the error returned is
TypeError: arguments did not match any overloaded call:
insertText(self, str): argument 1 has unexpected type 'QModelIndex'
insertText(self, str, QTextCharFormat): argument 1 has unexpected type 'QModelIndex'
The build_project()
code is below:
def build_project(self, item):
if item:
index = self.treeView.setCurrentIndex(item)
else:
index = self.fileSystemModel.filePath(self.treeView.currentIndex())
index_path = Path(index) # Convert string to path-type
parent_dir = index_path.parents[0] # Get the path to the /opencpi/projects directory
out = subprocess.run(["bash", "-c", f"source {self.default_dir}/cdk/opencpi-setup.sh -r && cd {parent_dir} && "
f"ocpidev build project {index_path.name}"],
stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
self.console_output.on_update_text(out.stdout.decode()) # Send command output to console
If I remove the console_output line in add_item()
and pass the index to build_project()
, I get the following error:
<removed for clarity>
...
File "/home/toor/PycharmProjects/ie-gui/OpenCPI_GUI.py", line 991, in build_project
index_path = Path(index) # Convert string to path-type
...
TypeError: expected str, bytes or os.PathLike object, not NoneType
I've tried a number of different things and can confirm that the index values generated by add_item()
are QModelIndex values. Since the singular creation of the index via self.treeView.currentIndex()
works, I don't understand why setCurrentIndex()
doesn't work, much less why it claims a None value is being provided.
PyQt/PySide 1:1 Coaching with Martin Fitzpatrick — Get one on one help with your Python GUI projects. Working together with you I'll identify issues and suggest fixes, from bugs and usability to architecture and maintainability.
PyQt6 Crash Course — a new tutorial in your Inbox every day
Beginner-focused crash course explaining the basics with hands-on examples.