首先,介绍一下Monaco Editor
The Monaco Editor is the code editor that powers VS Code. A good page describing the code editor’s features is here.
It is licensed under the MIT License and supports Edge, Chrome, Firefox, Safari and Opera.
The Monaco editor is not supported in mobile browsers or mobile web frameworks.
Find more information at the Monaco Editor repo.
我们的仿vscode代码编辑器功能基于Monaco Editor(VSCode的内核)实现,通过建立本地html并通过QWebEngineView嵌入QT,使用page().runJavaScript调用js实现代码的设置,并实现代码高亮、自动换行、自动补全等功能
嵌入QT
利用QWebEngineView
可以将网页嵌入QT,网页文件如下:
1 |
|
然后将网页嵌入QT中:
1 | self.editor_index = (os.path.split(os.path.realpath(__file__))[0]) + "/vscode.html" |
最后,通过调用Monaco支持的js完成网页内容赋值
1 | def set_value(self, data): |
效果:
在线编译
通过以下代码调用cmd即可
注意,需要先安装python并配置环境变量
1 | os.system(f'start cmd /K python {filepath}\{filename}') |