使用QT和Python中的QTableView中的QCompleter

时间:2014-07-25 02:11:31

标签: python qt qabstracttablemodel qcompleter

我正在阅读how to make my QAbstractTableModel editable,看起来非常简单。

但是如何设置可编辑单元格以使用QCompleter?我不知怎的,我必须告诉QTableView使用QLineEdit小部件?我怎么能这样做?


编辑:嗯,我想它与QTableView.setItemDelegateForColumn()有关,但我不了解delegates或如何使用它们。


编辑:我尝试了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 个答案:

答案 0 :(得分:2)

创建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 != 0){

                 // 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));
             }
         }
}; 

答案 1 :(得分: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

这是complete example as a github gist