QFileDialog作为TableView的编辑器:如何获得结果? [英] QFileDialog as editor for TableView: how to get result?

查看:96
本文介绍了QFileDialog作为TableView的编辑器:如何获得结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用QFileDialog作为QTableView中某些列的编辑器.这基本上可以正常工作(对某些焦点问题进行模数化,请参见此处):

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)

但是,当关闭编辑器时,我看不到区分ChooseCancel(或失去焦点)的方法,在所有情况下都调用setEditorData函数.我没有办法从QFileDialog中获得作为editor获得的结果,我可以找到的所有示例都使用了我无法访问的exec_的返回值.

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.

推荐答案

setModelData中,您似乎可以检查编辑器的

In setModelData, it looks like you could check the editor's result before setting the model's data. By default, the result is QDialog.Rejected, and that should only change if the user actually chooses a file:

    def setModelData(self, editor, model, index):
        if editor.result() == QtGui.QDialog.Accepted:
            model.setData(index, editor.selectedFiles()[0])   

更新:

经过一些迟来的测试,很明显,无论如何运行文件对话框(即使使用exec),其result也将永远不会在委托编辑器的上下文中正确重置.因此,需要一点间接性.根据 QFileDialog.filesSelected 的文档,此信号将始终并且仅在接受对话框时发送(即使没有选择的文件也是如此).因此,我们可以使用这种机制来强制正确的对话框结果,如下所示:

After some belated testing, it's obvious that no matter how the file-dialog is run (even with exec), its result will never get properly reset in the context of a delegate editor. So a little indirection is needed. According to the docs for QFileDialog.filesSelected, this signal will always and only be sent when the dialog is accepted (even if there are no selected files). So we can use this mechanism to force the correct dialog result, like this:

class DirectorySelectionDelegate(QtGui.QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        editor = QtGui.QFileDialog(parent)
        editor.filesSelected.connect(
            lambda: editor.setResult(QtGui.QDialog.Accepted))
        ...

    def setModelData(self, editor, model, index):
        print(editor.result())
        if editor.result() == QtGui.QDialog.Accepted:
            model.setData(index, editor.selectedFiles()[0])

这篇关于QFileDialog作为TableView的编辑器:如何获得结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆