Qt for Python/GettingStarted: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Formatting)
 
(82 intermediate revisions by 15 users not shown)
Line 1: Line 1:
== Considerations before starting ==
[[Category:Qt for Python]]                                                                                                                               
==Official documentation==


PySide2 supports Python 2 (recommended: 2.7 onwards, Compatibility module ''six'' installed) and Python 3 (recommended: 3.5 onwards).  
Refer to the [https://doc.qt.io/qtforpython/gettingstarted.html official docs] to start building and using Qt for Python.


Currently, only Qt 5.6 is supported. Qt needs to be build with the ''QtXmlPatterns'' module.
==Using Qt Creator as a project explorer==                                     
Qt Creator 4.0+ can be used to open the PySide and Shiboken CMakeLists.txt files as projects, and thus provide usual IDE features for developing PySide - project file navigation, code completion (C++ only), following symbols under cursor (C++ only), syntax highlighting, locator usage, debugging, etc.
                                                                                   
Currently, there is a limitation that Shiboken has to be built first using the terminal because the installed shiboken CMake packages will have to be specified for the PySide project in Qt Creator.


=== Development ===
The steps for opening the projects in Qt Creator are:                               


Development happens in the dev branches of the [http://code.qt.io/cgit/pyside/pyside-setup.git/ repositories]. The top level repository has several submodules:
#Open '''pyside-setup/sources/shiboken2/CMakeLists.txt''' and specify a 5.12+ Qt Kit to be used
* sources/shiboken2: [http://code.qt.io/cgit/pyside/shiboken.git/ Shiboken Parser]
#Build the project as usual (by pressing the '''build''' icon for instance)
* sources/pyside2: [http://code.qt.io/cgit/pyside/pyside.git/ PySide 2]
#Open '''pyside-setup/sources/pyside2/CMakeLists.txt''' and specify the same 5.12+ Qt Kit
* sources/pyside2-examples: [http://code.qt.io/cgit/pyside/examples.git/ Examples]
#Go to the projects tab, and under the Build / CMake section find the Shiboken2_DIR setting. You have to specify the path to the folder where the Shiboken CMake package was installed when you compiled Shiboken from the terminal·
* sources/pyside2-tools: uic, rcc
#An example path under macOS is '''/Users/user/Dev/pyside2-setup/pyside_install/py3.6-qt5.12.1-64bit-debug/lib/cmake/Shiboken2-2.0.0'''. The path has to be adjusted depending on the user folder name, the version of python and qt, etc
* wiki: Wiki
#(Optional) On MacOS you also have to set the '''ALTERNATIVE_QT_INCLUDE_DIR''' setting to the Qt kit include path (e.g. '''/Users/user/Dev/qt511_source/include''')
#Apply the CMake configuration changes (by pressing the button), and you should be able to build PySide
                                                                                   
Now you can use the project explorer to look through the source cpp files, python files, use the locator feature to open files and file classes / methods, and other features that Qt Creator provides.


Contributions follow the [[Qt_Project_Guidelines|standard process]].  
==Troubleshooting / Known Issues==
                                                                                   
*Qt 5.9 does not work with OpenSSL 1.1                                       
**When doing a custom Qt build (some unspecified versions for now), It is necessary to have an OpenSSL version of 1.0.x, since there are compatibility issues with newer versions of OpenSSL ([https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes#Qt see details])
*PySide2 looks at the system installation if the local Qt version does not have a required module
**The only workaround is to uninstall any module from the system, then PySide2 can look at only the Qt path currently being use.
*Qt packages that directly link to OpenSSL (as opposed to runtime discovery) are not currently supported.
*Make sure that the Python environment location where the PySide2 package will be installed is writable (otherwise you might get various permission denied errors). The install location can be found with 99% probability by running:


Building requires [https://cmake.org/ CMake].  
python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"


It is helpful to have debug binaries and/or symbols for Python available. On Windows, this is done choosing ''Customized Installation'' when installing python and ticking the respective check boxes. On Linux, debug packages can be installed in addition. For Ubuntu, the packages python3-dbg, libpython3-dbg provide a debug binary python3-dbg.
*Building failing because graphviz wasn't found. If you're using pyenv and installed it using pip, try to install it using your package manager:<syntaxhighlight>
 
dnf install graphviz.x86_64 #Fedora
It is also recommended to use a [http://docs.python-guide.org/en/latest/dev/virtualenvs/ Virtual Environment] for testing to be able to always start from a clean base and avoid issues with write permissions in installations.
</syntaxhighlight>
 
*20220727 Urgent update: If you build PySide with a custom built Python, you should build python with the following options: <syntaxhighlight lang="shell">
On Linux, the command
PYTHON_CONFIGURE_OPTS="--enable-shared --with-trace-refs"
virtualenv -p /usr/bin/python3-dbg testenv
</syntaxhighlight>For example:<syntaxhighlight lang="shell">
creates a Virtual Environment named ''testenv'' for debugging purposes. On Windows, an installation step may be required:
PYTHON_CONFIGURE_OPTS="--enable-shared --with-trace-refs" pyenv install -kg 3.9.13
python -m pip install virtualenv
</syntaxhighlight>This makes PySide2 work without the limited API. <br />
python -m virtualenv testenv
 
The Virtual Environment is activated by
  source testenv/bin/activate
or
CALL testenv\Scripts\activate.bat
 
=== Building PySide2 ===
 
The script ''setup.py'' is used to build and install PySide2 package. It takes a mode argument (''build'' or ''install'') and several options.
 
Required options:
* --qmake=<binary> Path to ''qmake'' of the Qt library to be used
* --cmake=<binary> Path to ''cmake''
 
Further options of interest:
* ---ignore-git: Prevents ''setup.py'' from cloning and checking out the submodules.
* --build-tests: Creates a directory containing the tests along with some helper packages
* --debug: Build in Debug mode
* --openssl: Path to OpenSSL
 
A typical invokation looks like:
setup.py install ---ignore-git --build-tests --qmake=../bin/qmake --cmake=/usr/bin/cmake

Latest revision as of 14:08, 27 July 2022

Official documentation

Refer to the official docs to start building and using Qt for Python.

Using Qt Creator as a project explorer

Qt Creator 4.0+ can be used to open the PySide and Shiboken CMakeLists.txt files as projects, and thus provide usual IDE features for developing PySide - project file navigation, code completion (C++ only), following symbols under cursor (C++ only), syntax highlighting, locator usage, debugging, etc.

Currently, there is a limitation that Shiboken has to be built first using the terminal because the installed shiboken CMake packages will have to be specified for the PySide project in Qt Creator.

The steps for opening the projects in Qt Creator are:

  1. Open pyside-setup/sources/shiboken2/CMakeLists.txt and specify a 5.12+ Qt Kit to be used
  2. Build the project as usual (by pressing the build icon for instance)
  3. Open pyside-setup/sources/pyside2/CMakeLists.txt and specify the same 5.12+ Qt Kit
  4. Go to the projects tab, and under the Build / CMake section find the Shiboken2_DIR setting. You have to specify the path to the folder where the Shiboken CMake package was installed when you compiled Shiboken from the terminal·
  5. An example path under macOS is /Users/user/Dev/pyside2-setup/pyside_install/py3.6-qt5.12.1-64bit-debug/lib/cmake/Shiboken2-2.0.0. The path has to be adjusted depending on the user folder name, the version of python and qt, etc
  6. (Optional) On MacOS you also have to set the ALTERNATIVE_QT_INCLUDE_DIR setting to the Qt kit include path (e.g. /Users/user/Dev/qt511_source/include)
  7. Apply the CMake configuration changes (by pressing the button), and you should be able to build PySide

Now you can use the project explorer to look through the source cpp files, python files, use the locator feature to open files and file classes / methods, and other features that Qt Creator provides.

Troubleshooting / Known Issues

  • Qt 5.9 does not work with OpenSSL 1.1
    • When doing a custom Qt build (some unspecified versions for now), It is necessary to have an OpenSSL version of 1.0.x, since there are compatibility issues with newer versions of OpenSSL (see details)
  • PySide2 looks at the system installation if the local Qt version does not have a required module
    • The only workaround is to uninstall any module from the system, then PySide2 can look at only the Qt path currently being use.
  • Qt packages that directly link to OpenSSL (as opposed to runtime discovery) are not currently supported.
  • Make sure that the Python environment location where the PySide2 package will be installed is writable (otherwise you might get various permission denied errors). The install location can be found with 99% probability by running:
python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
  • Building failing because graphviz wasn't found. If you're using pyenv and installed it using pip, try to install it using your package manager:
    dnf install graphviz.x86_64 #Fedora
    
  • 20220727 Urgent update: If you build PySide with a custom built Python, you should build python with the following options:
    PYTHON_CONFIGURE_OPTS="--enable-shared --with-trace-refs"
    
    For example:
    PYTHON_CONFIGURE_OPTS="--enable-shared --with-trace-refs" pyenv install -kg 3.9.13
    
    This makes PySide2 work without the limited API.