Hello, good evening. I want to use word autocompletion in a QTreeview using the QCompleter class. The problem I have is that when I double-click on the text I want to edit, it is deleted when I use a delegate that contains a Qlineedit, which in turn contains autocompletion.
What I want to achieve is that when I double-click on the text, it is selected, not deleted. I tried to modify the double-click event within the delegate, but it doesn’t work. Below is a minimal example so you can see what I mean.
from PySide6.QtWidgets import QHBoxLayout,QMainWindow,QTreeView,QLineEdit,QStyledItemDelegate,QCompleter,QApplication
from PySide6.QtGui import QStandardItemModel,QStandardItem
from PySide6.QtCore import Qt,QEvent
import sys
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.setMinimumSize(800,600)
self.principal=Por_Hacer()
self.layout_principal=QHBoxLayout()
self.setCentralWidget(self.principal)
self.principal.setLayout(self.layout_principal)
class Por_Hacer(QTreeView):
def __init__(self):
super().__init__()
self.setStyleSheet("background-color:#FF313131;color:white;font:bold 16px;border:None;")
self.header().hide()
self.mi_delegate=Mi_delegate()
self.my_model=QStandardItemModel()
self.setModel(self.my_model)
#self.setItemDelegate(self.mi_delegate)
self.my_item1=QStandardItem()
self.my_item1.setText("Worker")
self.my_model.appendRow(self.my_item1)
class Mi_delegate(QStyledItemDelegate):
def __init__(self):
super().__init__()
def createEditor(self, parent, option, index):
editor=QLineEdit(parent)
return editor
def editorEvent(self, event, model, option, index,):
if event.type() == QEvent.MouseButtonDblClick:
event.ignore()
return super().editorEvent(event, model, option, index)
def setEditorData(self, editor, index):
completar=["a","e"]
completer = QCompleter(completar)
completer.setCaseSensitivity(Qt.CaseInsensitive)
editor.setCompleter(completer)
app=QApplication(sys.argv)
wind=Main()
wind.show()
app.exec()