在Qt和Python中在QTableView中使用QCompleter

1 投票
2 回答
1851 浏览
提问于 2025-04-18 14:42

我正在研究如何让我的 QAbstractTableModel 可编辑,看起来挺简单的。

但是我该如何设置一个可编辑的单元格来使用 QCompleter 呢?我想我需要告诉 QTableView 使用一个 QLineEdit 组件?我该怎么做呢?


编辑:嗯,我想这和QTableView.setItemDelegateForColumn() 有关,但我对委托一无所知,也不知道怎么使用它们。


编辑:我尝试了 RobbieE 的解决方案,得到了一些能用的东西,但弹出组合框的几何形状不对,按下 Enter 时 Python 会崩溃。

class CompleterDelegate(QtGui.QStyledItemDelegate):
    def __init__(self, parent=None, completerSetupFunction=None):
        super(CompleterDelegate, self).__init__(parent)
        self._completerSetupFunction = completerSetupFunction
    def createEditor(self, parent, option, index):
        return QtGui.QLineEdit(parent)
    def setEditorData(self, editor, index):
        super(CompleterDelegate, self).setEditorData(editor, index)
        self._completerSetupFunction(editor, index)

我的 _completerSetupFunction 看起来像这样:

def setupFunc(editor, index):
    completer = MyCompleter(editor)
    completer.setCompletionColumn(0)
    completer.setCompletionRole(QtCore.Qt.DisplayRole)
    completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)    
    editor.setCompleter(completer)
    completer.setModel(myAbstractItemModel)

2 个回答

3

创建一个 QStyledItemDelegate 的子类。

你只需要重新实现 setEditorData 这个函数,检查编辑器小部件是否是 QLineEdit,然后设置补全器。

请原谅我不太懂Python,但这是在C++中实现的方法。希望转换成Python会很简单。

class MyDelegate : public QStyledItemDelegate{
     public:
         void setEditorData(QWidget *editor, QModelIndex const &index){
             
             // call the superclass' function so that the editor widget gets the correct data
             QStyledItemDelegate::setEditorData(editor, index);

             // Check that the editor passed in is a QLineEdit. 
             QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);

             if (lineEdit != nullptr){

                 // add whatever completer is needed, making sure that the editor is the parent QObject so it gets deleted along with the editor
                 lineEdit.setComplete(new MyCompleter(editor));
             }
         }
}; 
2

根据RobbieE的建议,我创建了一个QStyledItemDelegate的子类。不过,正确的地方是在编辑器创建的时候应用补全功能,而不是在setEditorData里。

class CompleterDelegate(QtGui.QStyledItemDelegate):
    def __init__(self, parent=None, completerSetupFunction=None):
        super(CompleterDelegate, self).__init__(parent)
        self._completerSetupFunction = completerSetupFunction
    def createEditor(self, parent, option, index):
        editor = QtGui.QLineEdit(parent)
        self._completerSetupFunction(editor, index)
        return editor

然后我使用了一个叫completerSetupFunction的函数,基本上看起来是这样的:

def _completerSetupFunction(editor, index):
    print "completer setup: editor=%s, index=%s" % (editor, index)
    completer = QtGui.QCompleter(base_items, editor)
    completer.setCompletionColumn(0)
    completer.setCompletionRole(QtCore.Qt.EditRole)
    completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
    try:    
        editor.setCompleter(completer)            
    except:
        pass

这里有一个完整的例子,放在github上

撰写回答