我有一个QTableView
与相关的QAbstractTableModel
其中某些列中包含目录名称。我想用一个QFileDialog
作为编辑来更改这些列。这有点不寻常,因为编辑器不会位于表格单元格内(没有足够的空间)。
我使用了基础知识QStyledItemDelegate
:
class DirectorySelectionDelegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QFileDialog(parent)
editor.setFileMode(QFileDialog.Directory)
editor.resize(400, 400)
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())
当双击该单元格时,它会启动QFileDialog
,我可以选择我想要的目录,然后在模型中设置选择它。
然而,如果由于某种原因QFileDialog
失去焦点则关闭,并且数据设置为原始值。我希望对话框一直打开,直到用户单击“取消”或“选择”,但我找不到方法来做到这一点。
奖金问题:由于某种原因,对话框忽略了resize()
调用并启动得非常小(这使得更有可能失去焦点)。如何更改对话框的大小?