I am using QCompleter to populate a QLineEdit widget lineEdit1
with a list of auto completions autocomplete_list
. The widget successfully populates with the data in autocomplete list.
However when I enter the any of the following text into lineEdit1
:
a
a1
a2
a3
a4
a5
And presss return, I get the following error message:
Process finished with exit code -1073741819 (0xC0000005)
What is happening here? The program doesn't crash when I enter 'b' into the widget. It is also fine when I enter '1' into the widget. Why does it crash when I enter 'a' or any of the 5 items in autocomplete_list
?
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def setupData(self):
self.lineEdit1 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit1.setObjectName("lineEdit1")
self.lineEdit1.returnPressed.connect(self.return_pressed)
self.autocomplete_list = ['a1', 'a2', 'a3', 'a4', 'a5']
self.completer = QtWidgets.QCompleter(self.autocomplete_list)
self.lineEdit1.setCompleter(self.completer)
def return_pressed(self):
user_input = self.lineEdit1.text()
updated_list = [x for x in self.autocomplete_list if x not in user_input]
print(updated_list)
self.completer = QtWidgets.QCompleter(updated_list)
self.lineEdit1.setCompleter(self.completer)
print('Gets to here')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
ui.setupData()
MainWindow.show()
sys.exit(app.exec_())
I have tested logic in C++ yourself and I don't see any errors which I can suppose the error is caused by the sip wrapper that keeps a reference to the pointer of the old QCompleter that is removed from memory when you establish a new QCompleter generating a Segmentation fault ( core dumped).
On the other hand, your methodology is not very elegant, I prefer to use a model to handle the information and then only remove the items in a simple way.
def setupData(self):
self.lineEdit1 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit1.setObjectName("lineEdit1")
self.lineEdit1.returnPressed.connect(self.return_pressed)
autocomplete_list = ['a1', 'a2', 'a3', 'a4', 'a5']
self.autocomplete_model = QtGui.QStandardItemModel()
for text in autocomplete_list:
self.autocomplete_model.appendRow(QtGui.QStandardItem(text))
self.completer = QtWidgets.QCompleter()
self.completer.setModel(self.autocomplete_model)
self.lineEdit1.setCompleter(self.completer)
def return_pressed(self):
user_input = self.lineEdit1.text()
items = self.autocomplete_model.findItems(user_input, QtCore.Qt.MatchContains)
for item in items:
self.autocomplete_model.takeItem(item.row())