50

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.

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.

Pieter
  • 31,619
  • 76
  • 167
  • 242
  • 1
    [This page](http://askubuntu.com/questions/264037/how-to-use-qt-creator-with-python) and [this one](http://pythonthusiast.pythonblogs.com/230_pythonthusiast/archive/1358_developing_cross_platform_application_using_qt_pyqt_and_pyside__gui_application_development-part_5_of_5.html) suggest that although you can use QtCreator somewhat with Python, it's not a seamless experience. That second page gives an example of creating a Python Qt app, but it's not all done within QtCreator. – BrenBarn Jun 07 '14 at 19:12
  • There is a tut here on using QtCreator with Pyside, http://qt-project.org/wiki/QtCreator_and_PySide – Padraic Cunningham Jun 07 '14 at 19:30
  • 1
    @PadraicCunningham: as I understand it, this method manually generates Python code for my UI classes by reading *.ui* files. I'd have to manually run that command every time I update any part of the GUI and any custom methods that I added to UI classes will be discarded on overwrite. Is this correct? Because I don't think that would be a practical workflow. – Pieter Jun 07 '14 at 19:46
  • @BrenBarn: hmm... sounds like they haven't achieved full support yet. That's not the news I wanted to hear, but maybe in a few months they'll be ready for it. – Pieter Jun 07 '14 at 19:53
  • @Pieter, that does seem to be the case, as you already know the python support seems pretty limited for now. I guess the qt creator forums would be the best place to find what you need. – Padraic Cunningham Jun 07 '14 at 20:53
  • There are ways to make it a more tolerable experience. First off, consider using a QML interface and then talking with that programatically. Its not that well documented, but it works, and keeps a wall of separation between the UI and logic. Alternatively, you could use the autogenerated python, and then *subclass* from that, so that upstream changes will not aversely hurt downstream code as long as care is taken to manage changes to the class contrace sanely. – Shayne Apr 10 '17 at 07:40

2 Answers2

47

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.

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:

  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

You get something like this:

enter image description here

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

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:

  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

Then you should have PyUic connected as well.

Jed Daniels
  • 24,376
  • 5
  • 24
  • 24
NorthCat
  • 9,643
  • 16
  • 47
  • 50
8

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.

Here are the build settings:

qtcreator python build settings

Here are the run settings:

qtcreator python run settings

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

Here are the project file settings:

qtcreator python project settings

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.

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)...

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

qtcreator python pdb settings

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

qtcreator run button

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:

if (QtCore.QProcessEnvironment.systemEnvironment().value("DEBUG") == "1"):
                import pdb; QtCore.pyqtRemoveInputHook(); pdb.set_trace()
miller the gorilla
  • 860
  • 1
  • 7
  • 19