PySide
Einführung
Die PySide Bibliothek ermöglicht den Zugriff auf den plattformübergreifenden grafischen Benutzeroberflächen Werkzeugsatz (GUI) Qt. Qt ist eine Sammlung von C++ Bibliotheken, aber mit Hilfe von PySide können die gleichen Komponenten von Python aus verwendet werden. Jede grafische Schnittstelle, die in C++ erstellt werden kann, kann auch in Python erstellt und modifiziert werden. Ein Vorteil der Verwendung von Python ist, dass Qt Oberflächen entwickelt und live getestet werden können, da wir die Quelldateien nicht kompilieren müssen.
Wenn du Installation FreeCAD, solltest du sowohl Qt als auch PySide als Teil des Pakets erhalten. Wenn du selbst Kompilierst, dann musst du überprüfen, ob diese beiden Bibliotheken installiert sind, damit FreeCAD korrekt läuft. Natürlich wird PySide nur funktionieren, wenn Qt vorhanden ist.
In the past, FreeCAD used PyQt, another Qt binding for Python, but in 2013 (1dc122dc9a) the project migrated to PySide because it has a more permissible license.
For more information see:
Examples created with PySide. Left: a simple dialog. Right: a more complex dialog with graphs.
PySide in FreeCAD mit Qt5
FreeCAD was developed to be used with Python 2 and Qt4. As these two libraries became obsolete, FreeCAD transitioned to Python 3 and Qt5. In most cases this transition was done without needing to break backwards compatibility.
Normally, the PySide
module provides support for Qt4, while PySide2
provides support for Qt5. However, in FreeCAD, there is no need to use PySide2
directly, as a special PySide
module is included to handle Qt5.
This PySide
module is located in the Ext/
directory of an installation of FreeCAD compiled for Qt5.
/usr/share/freecad/Ext/PySide
This module just imports the necessary classes from PySide2
, but places them in the PySide
namespace. This means that in most cases the same code can be used with both Qt4 and Qt5, as long as it imports the single PySide
module.
PySide2.QtCore -> PySide.QtCore
PySide2.QtGui -> PySide.QtGui
PySide2.QtSvg -> PySide.QtSvg
PySide2.QtUiTools -> PySide.QtUiTools
The only unusual aspect is that the PySide2.QtWidgets
classes are placed in the PySide.QtGui
namespace.
PySide2.QtWidgets.QCheckBox -> PySide.QtGui.QCheckBox
Beispiele für PySide Verwendung
- Beginner PySide Examples (Hello World, announcements, enter text, enter number)
- Medium PySide Examples (window sizing, hiding widgets, popup menus, mouse position, mouse events)
- Advanced PySide Examples (widgets etc.)
Die zu behandelnde Materie wird dabei auf drei Teile aufgeteilt, die sich in der Detailtiefe unterscheiden mit der PySide, Python und die FreeCAD-Interna dargestellt werden. Der erste Teil enthält einen Überblick und Hintergrundmaterial zu PySide und wie es funktioniert, während der zweite und dritte Teil hauptsächlich Beispiele für Programmcode auf verschiedenen Leveln enthalten.
Die Intention ist, dass die zugehörigen Seiten einfachen Pythoncode zur Anwendung von PySide vorstellen, der es einem Anwender erlaubt, sein Problem durch einfaches Kopieren und Einfügen der Beispiele zu lösen, ohne allzusehr von der eigentlichen Aufgabenstellung in FreeCAD abzulenken. Gegebenenfalls sind nur noch kleinere Anpassungen vorzunehmen. Die Hoffnung ist, dass damit nicht mehr das ganze Internet auf der Suche nach Antworten auf PySide-Fragen durchforstet werden muss. Gleichzeitig sollen jedoch damit nicht die zahlreichen im Web vorhandenen kompakten PySide-Tutorials und Dokumentationsseiten überflüssig gemacht werden.
Documentation
There are some differences in handling of widgets in Qt4 (PySide) and Qt5 (PySide2). The programmer should be aware of these incompatibilities, and should consult the official documentation if something doesn't seem to work as expected on a given platform. Nevertheless, Qt4 is considered obsolete, so most development should target Qt5 and Python 3.
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 (Qt5).
- 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