在 Qt Creator 中开发 Python 应用程序 [英] Developing Python applications in Qt Creator
问题描述
我过去使用 Qt Creator 用 C++ 开发了一些 Qt 项目,但现在我想尝试 Qt 的 Python 实现.我发现 Qt Creator 2.8 及更高版本支持 Python,但到目前为止我还没有弄清楚如何用 Python 创建一个 Qt 应用程序.关于它的在线文档似乎很少.
I've developed a few Qt projects in C++ using Qt Creator in the past, but now I want to experiment with the Python implementation of Qt. I discovered that Qt Creator 2.8 and higher support Python, but I haven't been able to figure out how to create a Qt application in Python with it so far. Online documentation about it appears to be scarce.
如何在 Qt Creator 中设置这样的项目?理想情况下,我正在寻找一个简单的Hello World"项目,我可以在 Qt Creator 中打开该项目并将其用作构建某些东西的起点.
How do I set up such a project in Qt Creator? Ideally I'm looking for a simple "Hello World" project that I can open in Qt Creator and use that as a starting point to build something.
推荐答案
目前,Qt Creator
允许您创建 Python 文件(不是项目)并运行它们.它也有语法高亮,但它缺乏更复杂的功能,如自动完成.
Currently, Qt Creator
allows you to create Python files (not projects) and run them. It also has syntax highlighting, but it lacks more complex features such as autocomplete.
运行脚本需要一些配置(我使用了this教程).打开 Qt Creator
并转到 Tools->Options->Environment->External Tools
.单击Add->Add category
并创建一个新类别(例如,Python
).然后,选择创建的类别并单击Add->Add Tool
以创建一个新工具——例如RunPy
.选择创建的工具并填写右侧的字段:
Running scripts requires some configuration (I used this tutorial). Open Qt Creator
and go to Tools->Options->Environment->External Tools
. Click Add->Add category
and create a new category (for example, Python
). Then, select the created category and click Add->Add Tool
to create a new tool - RunPy
for example. Select the created tool and fill the fields on the right:
- 描述 - 任何值
- 可执行文件 -
python.exe
的路径 - 参数 -
%{CurrentDocument:FilePath}
- 工作目录 -
%{CurrentDocument:Path}
- 环境 -
QT_LOGGING_TO_CONSOLE=1
你会得到这样的东西:
现在,转到 File->New File or Project->Python
并选择 Python 源文件
.要运行创建的脚本:Tools->External->Python->RunPy
.
Now, go to File->New File or Project->Python
and select Python source file
. To run the created script: Tools->External->Python->RunPy
.
你也可以用同样的方式添加 pyuic:再次点击 Add->Add Tool
按钮来创建一个新工具 - PyUic
现在.再次选择它并填写右侧的字段:
You can also add pyuic to it the same way:
Click again on the Add->Add Tool
button to create a new tool - PyUic
now. Select it again and fill the fields on the right:
- 描述 - 任何值
- 可执行文件 -
pyuic5
的路径 - 参数 -
-o UI%{CurrentDocument:FileBaseName}.py -x %{CurrentDocument:FilePath}
- 工作目录 -
%{CurrentDocument:Path}
- 环境 -
QT_LOGGING_TO_CONSOLE=1
那么你也应该连接 PyUic.
Then you should have PyUic connected as well.
这篇关于在 Qt Creator 中开发 Python 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!