在 Qt Creator 中开发 Python 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24100602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:58:36  来源:igfitidea点击:

Developing Python applications in Qt Creator

pythonqt

提问by Pieter

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 用 C++ 开发了一些 Qt 项目,但现在我想尝试 Qt 的 Python 实现。我发现 Qt Creator 2.8 及更高版本支持 Python,但到目前为止我还没有弄清楚如何用 Python 创建 Qt 应用程序。关于它的在线文档似乎很少。

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 中设置这样的项目?理想情况下,我正在寻找一个简单的“Hello World”项目,我可以在 Qt Creator 中打开该项目并将其用作构建某些东西的起点。

采纳答案by NorthCat

Currently, Qt Creatorallows you to create Python files (not projects) and run them. It also has syntax highlighting, but it lacks more complex features such as autocomplete.

目前,Qt Creator允许您创建 Python 文件(不是项目)并运行它们。它也有语法高亮,但它缺乏更复杂的功能,如自动完成。

Running scripts requires some configuration (I used thistutorial). Open Qt Creatorand go to Tools->Options->Environment->External Tools. Click Add->Add categoryand create a new category (for example, Python). Then, select the created category and click Add->Add Toolto create a new tool - RunPyfor example. Select the created tool and fill the fields on the right:

运行脚本需要一些配置(我使用了教程)。打开Qt Creator并转到Tools->Options->Environment->External Tools。单击Add->Add category并创建一个新类别(例如,Python)。然后,选择创建的类别并单击Add->Add Tool以创建新工具 -RunPy例如。选择创建的工具并填写右侧的字段:

  1. Description - any value
  2. Executable - path to python.exe
  3. Arguments - %{CurrentDocument:FilePath}
  4. Working directory - %{CurrentDocument:Path}
  5. Environment - QT_LOGGING_TO_CONSOLE=1
  1. 描述 - 任何值
  2. 可执行文件 - 路径 python.exe
  3. 参数 - %{CurrentDocument:FilePath}
  4. 工作目录—— %{CurrentDocument:Path}
  5. 环境 - QT_LOGGING_TO_CONSOLE=1

You get something like this:

你会得到这样的东西:

enter image description here

在此处输入图片说明

Now, go to File->New File or Project->Pythonand select Python source file. To run the created script: Tools->External->Python->RunPy.

现在,转到File->New File or Project->Python并选择Python source file。要运行创建的脚本:Tools->External->Python->RunPy

You can also add pyuic to it the same way: Click again on the Add->Add Toolbutton to create a new tool - PyUicnow. Select it again and fill the fields on the right:

你也可以用同样的方式向它添加 pyuic:再次点击Add->Add Tool按钮来创建一个新工具 -PyUic现在。再次选择它并填写右侧的字段:

  1. Description - any value
  2. Executable - path to pyuic5
  3. Arguments - -o UI%{CurrentDocument:FileBaseName}.py -x %{CurrentDocument:FilePath}
  4. Working directory - %{CurrentDocument:Path}
  5. Environment - QT_LOGGING_TO_CONSOLE=1
  1. 描述 - 任何值
  2. 可执行文件 - 路径 pyuic5
  3. 参数 - -o UI%{CurrentDocument:FileBaseName}.py -x %{CurrentDocument:FilePath}
  4. 工作目录—— %{CurrentDocument:Path}
  5. 环境 - QT_LOGGING_TO_CONSOLE=1

Then you should have PyUic connected as well.

那么你也应该连接 PyUic。

回答by miller the gorilla

Thanks for this, it helped enormously.

感谢这一点,它有很大帮助。

I set up a build and run section of a new kit for python, using your instructions, which seems to work quite well.

我使用您的说明为 python 设置了一个新套件的构建和运行部分,这似乎工作得很好。

Here are the build settings:

以下是构建设置:

qtcreator python build settings

qtcreator python 构建设置

Here are the run settings:

以下是运行设置:

qtcreator python run settings

qtcreator python 运行设置

note that I have /usr/bin/python as a link to /usr/bin/python3.6

请注意,我将 /usr/bin/python 作为指向 /usr/bin/python3.6 的链接

Here are the project file settings:

以下是项目文件设置:

qtcreator python project settings

qtcreator python项目设置

The only thing that is necessary is to go into tools -> options -> build and run and unselect 'always build project before deploying it' and 'always deploy project before running it'.

唯一需要做的是进入工具 -> 选项 -> 构建并运行并取消选择“在部署之前始终构建项目”和“在运行之前始终部署项目”。

Once you have designed a form, you can then click build to create the UI.py file and run the currently selected python source file by clicking run.

设计好表单后,您可以单击构建以创建 UI.py 文件并通过单击运行运行当前选定的 Python 源文件。

For PyQt noobs like myself, I found the following resource to be particularly helpful at getting started... (although I am on linux rather than windows)...

对于像我这样的 PyQt 菜鸟,我发现以下资源对入门特别有帮助...(尽管我使用的是 linux 而不是 Windows)...

http://projects.skylogic.ca/blog/how-to-install-pyqt5-and-build-your-first-gui-in-python-3-4/

http://projects.skylogic.ca/blog/how-to-install-pyqt5-and-build-your-first-gui-in-python-3-4/

edit.

编辑。

I also added pdb - the python debugger

我还添加了 pdb - python 调试器

qtcreator python pdb settings

qtcreator python pdb 设置

which you can then select by clicking the button above the run button:

然后您可以通过单击运行按钮上方的按钮来选择它:

qtcreator run button

qtcreator 运行按钮

before clicking run. You can set breakpoints in your code using the following snippet, where I have added DEBUG = 1 to the system environment in the run settings of the pdb run and DEBUG = 0 to the run python env:

在点击运行之前。您可以使用以下代码段在代码中设置断点,其中我在 pdb run 的运行设置中将 DEBUG = 1 添加到系统环境,并将 DEBUG = 0 添加到 run python env:

if (QtCore.QProcessEnvironment.systemEnvironment().value("DEBUG") == "1"):
                import pdb; QtCore.pyqtRemoveInputHook(); pdb.set_trace()