1

QAbstractTableModel editable を作成する方法を読んでいますが、かなり簡単に見えます。

しかし、編集可能なセルを設定して 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)
4

2 に答える 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));
             }
         }
}; 
于 2014-07-25T07:05:41.890 に答える