Reputation: 119
This is my sample program for Qlineedit Completer. After Autosuggestion i want to display the text as starting of the item in line edit for that i wrote completer.activated.connect(lambda: QTimer.singleShot(0, lambda: edit.home(False)))
. Its working fine but i dont want to show cursor at the begging and last after autosuggestion.
Given below is my code:
import sys
from PyQt4.QtCore import Qt,QTimer
from PyQt4.QtGui import QApplication, QCompleter, QLineEdit, QStringListModel
def get_data(model):
model.setStringList(["completionProgramxxxxxxxxxxxxxxx", "completionProgramyyyyyy","truthordisabled", "storecredit", "iamhere"])
if __name__ == "__main__":
app = QApplication(sys.argv)
edit = QLineEdit()
completer = QCompleter()
edit.setCompleter(completer)
completer.activated.connect(lambda: QTimer.singleShot(0, lambda: edit.home(False)))
model = QStringListModel()
completer.setModel(model)
get_data(model)
edit.show()
sys.exit(app.exec_())
i got the image like this:
Upvotes: 2
Views: 513
Reputation: 244013
You have to clean the focus:
import sys
from PyQt4 import QtCore, QtGui
def get_data(model):
model.setStringList(
[
"completionProgramxxxxxxxxxxxxxxx",
"completionProgramyyyyyy",
"truthordisabled",
"storecredit",
"iamhere",
]
)
class Completer(QtGui.QCompleter):
def __init__(self, parent=None):
super(Completer, self).__init__(parent)
self.activated.connect(self.on_activated)
@QtCore.pyqtSlot()
def on_activated(self):
le = self.widget()
if isinstance(le, QtGui.QLineEdit):
le.home(False)
le.clearFocus()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
edit = QtGui.QLineEdit()
completer = Completer(edit)
edit.setCompleter(completer)
model = QtGui.QStringListModel(edit)
completer.setModel(model)
get_data(model)
edit.show()
sys.exit(app.exec_())
Upvotes: 2