0

I am trying to get the currently selected file in Qtreeview without utilizing an event/callback related to Qtreeview itself. So far I have only been able to get the filename but not the path.

 self.treeView.selectedIndexes()[0].data()

This just gives me the file...How do I get the full filepath?

This was as far as i got:

  self.model=QtWidgets.QFileSystemModel()
 self.model.setRootPath(QDir.currentPath())
  self.treeView.setModel(self.model)
ScottCov
  • 29
  • 4
  • Are you using a QFileSystemModel? – musicamante Jan 14 '22 at 19:44
  • Does this help? https://stackoverflow.com/questions/7047527/getting-the-currently-selected-item-in-qtreeview – Kovy Jacob Jan 14 '22 at 19:51
  • @KovyJacob No, the OP is asking about the *full* path, including the parent items. – musicamante Jan 14 '22 at 19:56
  • I realize QFileSystemmodel is probably the key here...just havent quite figured it out – ScottCov Jan 14 '22 at 20:12
  • @ScottCov So, are you using QFileSystemModel or not? Please use "@user" – musicamante Jan 14 '22 at 20:13
  • yes although will use whatever works best – ScottCov Jan 14 '22 at 21:13
  • @ScottCov Again, please use "@username" otherwise we won't get notifications when you answer to comments. Also, for future reference, always try to provide more details about what you're asking. The more you're clear the easier (and faster) answers can be given: if you specified that you were using QFileSystemModel since the beginning, you'd have received an answer 2 hours ago. Remember: you should *never* ask questions in a hurry; take your time to write *good* questions and you'll get good answers (and reputation points for upvotes). Please take your time to review the [tour] and read [ask]. – musicamante Jan 14 '22 at 21:30
  • @musicamante thank you very much...It works. Will read thru the links you sent. Thanks again! – ScottCov Jan 14 '22 at 22:05

1 Answers1

0

QFileSystemModel can return a QFileInfo object (a system-independent interface for file informations) for a given index using fileInfo(index).

The full path can then be retrieved with absoluteFilePath():

    index = self.treeView.selectedIndexes()[0]
    info = self.treeView.model().fileInfo(index)
    print(info.absoluteFilePath())
musicamante
  • 41,230
  • 6
  • 33
  • 58