PySide
PySide
PySide este un instrument Python multiplatformă obligatoriu pentru a crea GUI în QT. FreeCAD utilizează PySide pentru toate GUI (Graphic User Interface) în interiorul Python. PySide este o alternativă la pachetul PyQt folosit anterior de FreeCAD pentru GUI. PySide are o licență mai permisivă. A se vedea Differences Between PySide and PyQt for more information on the differences.
The recommended way to import PySide (of any version) in FreeCAD is:
from PySide import QtCore, QtGui, QtWidgets
Qt & PySide versions
- Version 1.0 of Qt was released in mid 1990’s
- Version 4.0 of Qt was released in 2005 and the 4.x series was officially retired in 2015
- The python wrapper for Qt4.x has the name PySide
- Version 5.0 of Qt was released in 2012 and the 5.x series was officially retired in 2020.
- The python wrapper for Qt5.x has the name PySide2
- Version 6.0 of Qt was released in 2020, it is the current version-series
- The python wrapper for Qt6.x has the name PySide6
Examples created with PySide. Left: a simple dialog. Right: a more complex dialog with graphs.
FreeCAD and PySide
The original idea behind FreeCAD was to bring Cas.CADE (geometric kernel), Qt (gui toolkit) and Python together to create a free 3D parametric CAD program with a built-in scripting engine. This was in the early 2000’s, i.e. prior to the existence of PySide, thus a different wrapper – PyQt – was used for the first good decade of FreeCAD’s life. PySide came about to solve licensing issues with PyQt, and the switch for FreeCAD to PySide was made in 2013 (commit 1dc122dc9a).
If you would like to know which Qt version your installation uses, the About dialog has version listings where Qt is included.
For more information see:
When you install FreeCAD, you will get both Qt and a matching PySide as part of the package. If you are compiling yourself, you must verify that matching versions of these two libraries are installed in order for FreeCAD to run correctly. Of course, PySide will only work if Qt is present.
Multi version compatibility
The main difference between the PySide and subsequent PySide2 and PySide6 libraries is the namespacing. PySide (Qt4) has the main namespaces QtGui and QtCore, whereas PySide2 (Qt5) and PySide6 (Qt6) introduced the additional namespace QtWidgets, where all the widgets are now located, they used to be in QtGui namespace.
FreeCAD’s approach to handle the different versions of Qt/PySide is to call them all PySide, which really should be thought of as PySideX. It does not matter which Qt/PySide version is installed, they are all imported with PySide.
A shim handles the differences between the versions. This PySide shim is located in the Ext/ directory of an installation of FreeCAD compiled for Qt5/Qt6.
/usr/share/freecad/Ext/PySide
This module just imports the necessary classes from PySide2/6, and places them in the PySide namespace. This approach was chosen for it's ability to provide backwards compatibility from a Qt4 perspective.
PySide2.QtCore -> PySide.QtCore
PySide2.QtGui -> PySide.QtGui
PySide2.QtWidgets -> PySide.QtWidgets
PySide2.QtSvg -> PySide.QtSvg
PySide2.QtUiTools -> PySide.QtUiTools
For this reason the PySide2.QtWidgets classes are also placed in the PySide.QtGui namespace.
PySide2.QtWidgets.QCheckBox -> PySide.QtGui.QCheckBox
But the current recommended way is to use the modern namespacing.
from PySide import QtCore, QtGui, QtWidgets
my_check_box = QtWidgets.QCheckBox()
Examples of PySide use
- Beginner PySide Examples (Hello World, anunțuri, introducerea textului, intgroducerea numerului)
- PySide Intermediate Examples (mărimea ferestrei, ascunderea widgets, meniuri popup, poziția mouse , evenimentele mouse-ului)
- Advanced PySide Examples (widgets etc.)
Acestea împart subiectul în 3 părți, diferențiate după nivelul de cunoaștere a PySide, Python și FreeCAD. Prima pagină are o imagine de ansamblu și un material de referință care oferă o descriere a PySide și modul în care sunt setate împreună, în timp ce a doua și a treia pagină sunt în mare parte exemple de cod la diferite niveluri.
Intenția este ca paginile asociate să furnizeze un cod Python simplu pentru a rula PySide, astfel încât utilizatorul care lucrează la o problemă să poată copia cu ușurință codul, să-l lipsească în munca proprie, să-l adapteze după cum este necesar și să se întoarcă la rezolvarea problemelor cu FreeCAD. Sperăm că nu trebuie să meargă pe Internet în căutarea răspunsurilor la problemele PySide. În același timp, această pagină nu are intenția de a înlocui diferitele tutoriale și site-uri de referință PySide disponibile pe web.
Documentation
There are some differences in the handling of widgets between the different versions. Programmers should be aware of these incompatibilities, and consult the official documentation if something doesn't work as expected on a given platform. Although rare, sometimes it may be required to make versioned calls through if-clauses.
The PySide documentation refers to the Python-style classes; however, since Qt is originally a C++ library, the same information should be available in the corresponding C++ reference.
- Qt Modules available from PySide2/PySide6 (Qt5/Qt6).
- All Qt classes by module in Qt5 for C++.
- Qt Modules available from PySide (Qt4).
- FreeCAD scripting: Python, Introduction to Python, Python scripting tutorial, FreeCAD Scripting Basics
- Modules: Builtin modules, Units, Quantity
- Workbenches: Workbench creation, Gui Commands, Commands, Installing more workbenches
- Meshes and Parts: Mesh Scripting, Topological data scripting, Mesh to Part, PythonOCC
- Parametric objects: Scripted objects, Viewproviders (Custom icon in tree view)
- Scenegraph: Coin (Inventor) scenegraph, Pivy
- Graphical interface: Interface creation, Interface creation completely in Python (1, 2, 3, 4, 5), PySide, PySide examples beginner, intermediate, advanced
- Macros: Macros, How to install macros
- Embedding: Embedding FreeCAD, Embedding FreeCADGui
- Other: Expressions, Code snippets, Line drawing function, FreeCAD vector math library (deprecated)
- Hubs: User hub, Power users hub, Developer hub

