作为TableView的编辑器的QFileDialog在丢失焦点时关闭 [英] QFileDialog as editor for TableView closes when losing focus

查看:212
本文介绍了作为TableView的编辑器的QFileDialog在丢失焦点时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QTableView 与相关联的 QAbstractTableModel ,其中包含一些列中的目录名称。我想使用一个 QFileDialog 作为编辑器来更改这些列。这有点不寻常,因为编辑器不会在表格单元格内(没有足够的空间)。

I have a QTableView with associated QAbstractTableModel that contains directory names in some columns. I would like to use a QFileDialog as the editor to change those columns. This a little unusual, as the editor is not going to be inside the table cell (not enough space).

我使用一个 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 ,我可以选择我想要的目录,并选择它在模型中设置。

When double-clicking the cell it starts a QFileDialog, I can select the directory I want and on Choose it is set in the model.

但是,如果由于任何原因,$ code> QFileDialog 失去焦点,它被关闭,数据设置为原始值。我更喜欢对话框打开,直到用户点击取消或选择,但我找不到办法。

However, if for whatever reason the QFileDialog loses focus it is closed, and the data is set to the original value. I would prefer the dialog to be open until the user clicks Cancel or Choose, but I cannot find a way to do that.

奖金问题: resize()调用并启动非常小(这使失去焦点更有可能)。如何更改对话框的大小?

Bonus question: for some reason the dialog ignores the resize() call and starts up very small (which makes losing the focus all the more likely). How can I change the size of the dialog?

推荐答案

这是预期的行为。

标准视图不是小部件的容器,必要时绘制每个单元格。用于编辑的小部件只有在生成编辑触发时才创建并叠加在视图的顶部。然后调用委托创建相应的编辑小部件,并将单元格的位置和大小作为参数传入。

The standard views are not containers of widgets, each cell is drawn when necessary. The widget used for editing is only created and superimposed on top of the view whenever an editing trigger is generated. The delegate is then called to create the appropriate editing widget and the location and size of the cell are passed in as an argument.

视图保留编辑器小部件的所有权,因为每当重点丢失时,您显然无法编辑,因此视图会删除编辑器。

The view retains ownership of the editor widget because, whenever focus is lost, you can obviously not be able to edit anymore so the view deletes the editor.

如果您没有在 setModelData 函数,模型不会被更新。说这个模型是回到原来的数据是不正确的,因为它从来没有被改变。首先,这个模型是不正确的。

If you do not pass anything back in the setModelData function, the model will not be updated. It is not correct to say that the model is set back to the original data because it never gets changed in the first place.

我建议你尝试设置 QFileDialog 以模态方式打开( editor.setModal(true)),以便在焦点之前必须关闭对话框转移到另一个小部件。

What I would suggest you try is setting the QFileDialog to open modally (editor.setModal(true)) so that the dialog has to be closed before focus can be transferred to another widget.

这篇关于作为TableView的编辑器的QFileDialog在丢失焦点时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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