Important: Please read the Qt Code of Conduct -

QPlainTextEdit as a Python editor?

  • I'm don't really know what I should be looking for so I some help. I have a QPlainTextEdit that I want it to act as a Python editor. The main usage for it is to catch errors, warnings and print command when I run the script. I would also like it to be aware of the variable's type in the script so to provide the corresponded attributes and methods as autocompleter.

    For example, when I define a as a string, entering . after will trigger the autocompleter for all of its methods.

    a = "hello"

    Where do I need to look?

  • Lifetime Qt Champion


    You can check the QSyntaxHighlighter example but this will require a QTextEdit.

    However, from the looks of your description, you seem to want to have support for a language server.

  • @SGaist
    Thanks, I checked out the Python language server by Microsoft, looks like it's the right path. But what about catching errors and warnings? What do I use?

  • Lifetime Qt Champion

    Take a look at Jedi

  • Also maybe its worth taking a look at QScintilla which has build in functionality for auto-completion, highlighting and those things.
    Here a link to some overview and here to the download

  • After testing out some Python codes in Visual Studio Code (it has Python language server implemented), I don't really like what I see. Some methods from my imported modules has a return type(class) and the autocompleter was not able to feed me the attributes and methods under that type. For example, the method myModule.getNewClass() will return a variable with type NewClass. And NewClass has a method called getName(),

    output = myModule.getNewClass()

    But then when I went like output., the autocompleter, instead of giving me getName() as the choice, it fed me all the methods from all the import modules.

    So this route is a no go for me.

  • If the auto-completer is not working the reason might be, that its the wrong format. There are different ways to define doc strings and how they are stored, e.g. in VIM it uses Jedi but cannot see the docstrings in c-extensions, however you can make them available via stub files e.g. in pybind11 and so on...
    TLDR: its a bit complicated and not only depends on your autocompleter but also on how you add the docstrings to your python module.

Log in to reply