[PyQt] dynamic code completion

Phil Thompson phil at riverbankcomputing.com
Thu Sep 10 15:48:07 BST 2015


On 10 Sep 2015, at 5:25 am, oliver <oliver.schoenborn at gmail.com> wrote:
> 
> I'd like to have a way to dynamically define the code completion API based on context. Overriding updateAutoCompletionList seems logical place to do this, but the following doesn't work: 
> 
> from PyQt5 import Qsci, QtWidgets
> 
> class MyScriptApi(Qsci.QsciAPIs):
>     def updateAutoCompletionList(self, context: [str], options: [str]):
>         super().updateAutoCompletionList(context, options)
>         options.append('abcdefg')
> 
> class Window(Qsci.QsciScintilla):
>     def __init__(self):
>         Qsci.QsciScintilla.__init__(self)
>         lexer = Qsci.QsciLexerPython(self)
>         api = MyScriptApi(lexer)
>         api.add('aLongString')
>         api.add('aLongString.abcd')
>         api.add('aLongerString')
>         api.add('aDifferentString')
>         api.add('sOmethingElse')
>         api.prepare()
>         self.setAutoCompletionThreshold(1)
>         self.setAutoCompletionSource(Qsci.QsciScintilla.AcsAPIs)
>         self.setLexer(lexer)
> 
> if __name__ == "__main__":
>     import sys
>     app = QtWidgets.QApplication(sys.argv)
>     window = Window()
>     window.show()
>     app.exec_()
> 
> If the updateAutoCompletionList is not defined (the class body is just "pass"), typing "a" shows several matches in the popup, but if the method is as shown above, no matches get shown, eventhough the base class method is called by the derived class. A breakpoint via debugger shows that base class method does not put anything in the options list, nor does the base class method return anything. 

It's a bug in the Python bindings - should be fixed in tonight's snapshot.

You need to use updateAutoCompletionList() as follows...

    def updateAutoCompletionList(self, context: [str], options: [str]):
        options = super().updateAutoCompletionList(context, options)
        options.append('abcdefg')
        return options

Phil


More information about the PyQt mailing list