Qt for Python 入门

This page is focused on building Qt for Python from source, if you just want to install PySide2 with pip you need to run:

pip install pyside2
								

for more details, refer to our Quick Start guide. Additionally, you can check the FAQ related to the project.

一般要求

  • Python :3.5+ 和 2.7

  • Qt: 推荐 5.12+

  • libclang: The libclang library, recommended: version 6 for PySide2 5.12. Prebuilt versions of it can be downloaded here .

  • CMake: 需要 3.1+。

每平台指南

You can refer to the following pages for platform specific instructions:

A normal building command will look like this:

python setup.py install --qmake=/path/to/qmake \
                        --ignore-git \
                        --debug \
                        --build-tests \
                        --parallel=8 \
                        --make-spec=ninja \
                        --verbose-build \
                        --module-subset=Core,Gui,Widgets
									

Which will build and install the project with debug symbols, including the tests ,使用 ninja (instead of make), and considering only the module subset of QtCore, QtGUI and QtWidgets.

Other important options to consider are:
  • --cmake , to specify the path to the cmake binary,

  • --reuse-build , to rebuild only the modified files,

  • --openssl=/path/to/openssl/bin , to use a different path for OpenSSL,

  • --standalone , to copy over the Qt libraries into the final package to make it work on other machines,

  • --doc-build-online , to build documentation using the online template.

测试安装

Once the installation finishes, you will be able to execute any of our examples:

python examples/widgets/widgets/tetrix.py
									

运行测试

使用 --build-tests option will enable us to run all the auto tests inside the project:

python testrunner.py test > testlog.txt
									

注意

On Windows, don’t forget to have qmake in your path ( set PATH=E:\Path\to\Qt\5.14\msvc2017_64\bin;%PATH% )

You can also run a specific test (for example qpainter_test ) by running:

ctest -R qpainter_test --verbose
									

构建文档编制

The documentation is being generated using qdoc to get the API information, and also sphinx for the local Python related notes.

The system required libxml2 and libxslt , also on the Python environment, sphinx and graphviz need to be installed before running the installation process:

pip install graphviz sphinx
									

After installing graphviz dot command needs to be in PATH, otherwise, the process will fail. Installing graphviz system-wide is also an option.

Since the process rely on a Qt installation, you need to specify where the qtbase directory you will use with your qmake is located:

export QT_SRC_DIR=/path/to/qtbase
									

Once the build process finishes, you can go to the generated *_build/*_release/pyside2 directory, and run:

make apidoc
									

注意

apidoc make target builds offline documenation in QCH (Qt Creator Help) format by default. You can switch to building for the online use with the --doc-build-online configure option.

Finally, you will get a html directory containing all the generated documentation. The offline help files, PySide.qch and Shiboken.qch , can be moved to any directory of your choice. You can find Shiboken.qch in the build directory, *_build\*_release\shiboken2\doc\html .

查看离线文档编制

The offline documentation (QCH) can be viewed using the Qt Creator IDE or Qt Assistant, which is a standalone application for viewing QCH files.

To view the QCH using Qt Creator, following the instructions outlined in Using Qt Creator Help Mode . If you chose to use Qt Assistant instead, use the following command to register the QCH file before launching Qt Assistant:

assistant -register PySide.qch

注意

Qt Assistant renders the QCH content using the QTextBrowser backend, which supports a subset of the CSS styles, However, Qt Creator offers an alternative litehtml-based backend, which offers better browsing experience. At the moment, this is not the default backend, so you have to select the litehtml backend explicitly under the General tab in Qt Creator >> 工具 >> Options >> Help .

使用内部工具

A set of tools can be found under the tools/ directory inside the pyside-setup repository.

  • checklibs.py : Script to analyze dynamic library dependencies of Mach-O binaries. To use this utility, just run:

    python checklibs.py /path/to/some.app/Contents/MacOS/Some
    											

    This script was fetched from this repository .

  • create_changelog.py : Script used to create the CHANGELOG that you can find in the dist/ directory. Usage:

    python create_changelog.py -r 5.14.1 -v v5.14.0..5.14 -t bug-fix
    											
  • debug_windows.py : This script can be used to find out why PySide2 modules fail to load with various DLL errors like Missing DLL or Missing symbol in DLL.

    You can think of it as a Windows version of ldd / LD_DEBUG .

    Underneath it uses the cdb.exe command line debugger, and the gflags.exe tool, both installed with the latest Windows Kit.

    The aim is to ask users to run this script when they encounter PySide2 imports not working on Windows. The user should then provide the generated log file.

    Incidentally it can also be used for any Windows executables, not just Python. To use it just run:

    python debug_windows.py
    											
  • missing_bindings.py : This script is used to compare the state of PySide2 and PyQt5 regarding available modules and classses. This content is displayed in our wiki page , and can be used as follows:

    python missing_bindings.py --qt-version 5.14.1 -w all
    											

    Please keep in mind we rely on BeautifulSoup to parse the content, so you will be to install it besides PySide2 and PyQt5 (Including additional modules like DataVisualiztion, QtCharts, WebEngine, etc).