Autocomplete from any position in python -


i have requirement in need show autocomplete results in pyqt's qlineedit widget based on entered text @ position. far able start of text

for ex : "hi, slim shady" , here can see text in autocomplete when type "hi " need have functionality can enable me search between or position of sentence.

my code :-

from pyside import qtcore, qtgui  class autocompleteedit(qtgui.qlineedit):     def __init__(self, model, separator = ' ', addspaceaftercompleting = true):         super(autocompleteedit, self).__init__()         self._separator = separator         self._addspaceaftercompleting = addspaceaftercompleting         self._completer = qtgui.qcompleter(model)         self._completer.setwidget(self)         self.connect(                 self._completer,                 qtcore.signal('activated(qstring)'),                 self._insertcompletion)         self._keystoignore = [qtcore.qt.key_enter,                               qtcore.qt.key_return,                               qtcore.qt.key_escape,                               qtcore.qt.key_tab]      def _insertcompletion(self, completion):         """         event handler qcompleter.activated(qstring) signal,         called when user selects item in completer popup.         """         = len(completion) - len(self._completer.completionprefix())         extra_text = completion[-extra:]         if self._addspaceaftercompleting:             extra_text += ' '         self.settext(self.text() + extra_text)      def textundercursor(self):         text = self.text()         textundercursor = ''         = self.cursorposition() - 1         while >=0 , text[i] != self._separator:             textundercursor = text[i] + textundercursor             -= 1         return textundercursor      def keypressevent(self, event):         if self._completer.popup().isvisible():             if event.key() in self._keystoignore:                 event.ignore()                 return         super(autocompleteedit, self).keypressevent(event)         completionprefix = self.textundercursor()         if completionprefix != self._completer.completionprefix():             self._updatecompleterpopupitems(completionprefix)         if len(event.text()) > 0 , len(completionprefix) > 0:             self._completer.complete()         if len(completionprefix) == 0:             self._completer.popup().hide()       def _updatecompleterpopupitems(self, completionprefix):         """         filters completer's popup items show items         given prefix.         """         self._completer.setcompletionprefix(completionprefix)         self._completer.popup().setcurrentindex(                 self._completer.completionmodel().index(0,0))  if __name__ == '__main__':     def demo():         import sys         app = qtgui.qapplication(sys.argv)         values = ['@call', '@bug', '+qtodotxt', '+sqlvisualizer', 'hi, slim shady']         editor = autocompleteedit(values)         window = qtgui.qwidget()         hbox = qtgui.qhboxlayout()         hbox.addwidget(editor)         window.setlayout(hbox)         window.show()          sys.exit(app.exec_())      demo() 


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -