I'm using a QFileDialog
as the editor for some columns in a QTableView
. This basically works (modulo some focus issues, see here):
class DirectorySelectionDelegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QFileDialog(parent)
editor.setFileMode(QFileDialog.Directory)
editor.setModal(True)
return editor
def setEditorData(self, editor, index):
val = index.model().data(index, Qt.DisplayRole)
fs = val.rsplit(os.path.sep, 1)
if len(fs) == 2:
bdir, vdir = fs
else:
bdir = "."
vdir = fs[0]
editor.setDirectory(bdir)
editor.selectFile(vdir)
def setModelData(self, editor, model, index):
model.setData(index, editor.selectedFiles()[0])
def updateEditorGeometry(self, editor, option, index):
r = option.rect
r.setHeight(600)
r.setWidth(600)
editor.setGeometry(r)
However, when the editor is closed I don't see a way to differentiate between Choose
and Cancel
(or lost focus), the setEditorData
function is called in all cases. I don't see a way to get the result from QFileDialog
that I get as editor
, all examples I can find use the return value from exec_
, which I don't have access to.