setCurrentIndex() says it receives NoneType

Heads up! You've already completed this tutorial.

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:

python
    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

python
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:

python
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:

python
  <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.


Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

setCurrentIndex() says it receives NoneType was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.