在使用Qt和Python一个QTableView中使用QCompleter(Using a QCom

2019-10-20 13:39发布

我读了关于如何使我的QAbstractTableModel编辑 ,它看起来非常简单。

但我怎么设置使用QCompleter可编辑单元格? 我把它不知何故,我必须告诉使用QLineEdit的插件的QTableView中? 我怎样才能做到这一点?


编辑:嗯,我想这与一些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)

Answer 1:

创建的子类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));
             }
         }
}; 


Answer 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的要点 。



文章来源: Using a QCompleter in a QTableView with Qt and Python