PyQt LineEdit with readline completer? -


I am working on a command line tool for which I am now making a PyQT GUI using the Readline module I want to implement my current self-implementation, and put it in the QLineEdit text box. Is this possible? Do you have any recommendations?

Here is an example of the stuff working with the readline module:

  import reading values ​​= ['these', 'are', 'mai', ' Autocomplete ',' word '] completion = {} DIFF full (text, state): try: matches = closing except text [text]: the match = [value for value if text In value.upper ()] in full to try [text] = matches: Return matches [except state] IndexError: whie 1: No readline.set_completer (Completer) readline.parse_and_bind ( 'menu: Completed tab'): return text = raw_input ( '& Gt;') text.dostuff ()   

finally, if I can not get Reedlain module to work in QLineEdit widget, so I finally did <-> / p>

I can tell you that first off, new functionality To try around QCompleter and tucking it's huge pain. You have to be able to complete all Q Compiler's interface, and pull it around the actual code.

You must manually update a QStringListModel set at QCompleter, and provide the implementation of the given search prefix, to provide the total number of current completion and completion.

The work is consistent with the example that PopupCompletion mode:

  import re class ReadlineCompleter (QtGui.QCompleter) : def __init __ (self, completeFn, * args, ** kwargs): Super (ReadlineCompleter, self) .__ init __ (* args, ** kwargs) self._completer = completeFn self.setModel (QtGui.QStringListModel ()) self.update () def setCompletionPrefix (self, val): Super (ReadlineCompleter, self) .setCompletionPrefix (VAL) self.update () def currentCompletion (self): state = self.currentRow () completionCount return self._completionAt (state) DEF (self): State = 0, while this is true: Results = self._completionAt ( State) if no result: break state + = 1 return state def update (self): matches = [self._completionAt (i) xrange (self.completionCount ()) for self.model (). SetStringList (mail) def_completionAt (self, state): text = str (self.completionPrefix ()) #regex to split on any white space, or four sets + * / ^ () - match = Re.match (r) '^ (. *) ([\ S + * / ^ (- -] +) (. *) $', Text) if match: prefix, sep, text = match.groups () result = self .completer (str (text), state) if the results match: results = sep.join ([prefix, the result]) 'returns' returns if no other results   

notice _completionAt ( ) method, I wanted to find a separator pattern, that you wanted to have additional functionality Added to You can explicitly adjust it but it will separate the last part and will use that value to check for completion, then add the result again.

Use

Important. To implement the update, you need to connect to QLineEdit to complete the textchanged signal, otherwise any functionality from the functionality will be used in the Completer.

  line = QtGui.QLineEdit () computer application = ReadlineCompleter (Completer) comp.setCompletionMode (COMP .PupupCompletion) line.setCompleter (COMP) # Important Line.textChanged.connect (comp.setCompletionPrefix)   

Showing how other people have had to fill in functionality in a custom line editing, where they are fully standard of integer Go around the signal and triggered yourself, you can see it a little bit of your effort.

Comments