Difference between revisions of "PySide/en"

From FreeCAD Documentation
Jump to navigation Jump to search
(Updating to match new version of source page)
(Updating to match new version of source page)
Line 1: Line 1:
 
<languages/>
 
<languages/>
{{docnav|Pivy|FeaturePython Objects}}
+
{{Docnav
 +
|[[Pivy|Pivy]]
 +
|[[FeaturePython Objects|FeaturePython Objects]]
 +
}}
  
 
==Introduction==
 
==Introduction==
  
[http://en.wikipedia.org/wiki/PySide PySide] is a Python binding of the cross-platform graphic user interface (GUI) toolkit Qt. FreeCAD uses PySide for all GUI purposes inside of Python. PySide is an alternative to the PyQt binding which was previously used by FreeCAD. PySide has a more permissible licence. See [http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences Between PySide and PyQt] for more information on the differences.
+
[[PySide|PySide]] is a library that allows you to use the cross-platform graphical user interface (GUI) toolkit Qt with [[Python|Python]]. Qt is a C++ collection of libraries, so its components are normally available from C++, but with the help of PySide, we can use the same components from Python. This means that every complex graphical interface that we can image that can be created in C++, can also be created and modified in Python. An advantage of using Python is that Qt interfaces can be developed and tested live, as we don't need to compile the source files.
 +
 
 +
When you [[Installing|install]] FreeCAD, you should get both Qt and PySide as part of the package. If you are [[Compiling|compiling]] yourself, then you must verify that these two libraries are installed, in order for FreeCAD to run correctly. Of course, PySide will only work if Qt is present.
 +
 
 +
In the past, FreeCAD used PyQt, another Qt binding for Python, but in 2013 ([https://github.com/FreeCAD/FreeCAD/commit/1dc122dc9a 1dc122dc9a]) the project migrated to PySide because it has a more permissible [[licence|license]].
 +
 
 +
For more information see:
 +
* [http://en.wikipedia.org/wiki/PySide Wikipedia:PySide]
 +
* [http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences Between PySide and PyQt]
 +
 
 +
[[File:PySideScreenSnapshot1.jpg]] [[File:PySideScreenSnapshot2.jpg]]
 +
{{Caption|Examples created with PySide. Left: a simple dialog. Right: a more complex dialog with graphs.}}
  
 
== PySide in FreeCAD with Qt5 ==
 
== PySide in FreeCAD with Qt5 ==
Line 17: Line 31:
 
}}
 
}}
  
This module just imports the necessary classes from {{incode|PySide2}}, but places them in the {{incode|PySide}} namespace. This means that in most cases the same code can be used with both Qt4 and Qt5, as long as it imports {{incode|PySide}}.
+
This module just imports the necessary classes from {{incode|PySide2}}, but places them in the {{incode|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 {{incode|PySide}} module.
 
{{Code|code=
 
{{Code|code=
 
PySide2.QtCore -> PySide.QtCore
 
PySide2.QtCore -> PySide.QtCore
Line 30: Line 44:
 
}}
 
}}
  
== PySide information ==
+
== Real examples of PySide use ==
  
Users of FreeCAD often achieve everything using the built-in interface. But for users who want to customise their operations then the Python interface exists which is documented in the [[Python_scripting_tutorial|Python Scripting Tutorial]]. The Python interface for FreeCAD had great flexibility and power. For its user interaction Python with FreeCAD uses PySide, which is what is documented on this page.
+
* [[PySide Beginner Examples]], hello world, announcements, enter text, enter number.
 +
* [[PySide Intermediate Examples]], window sizing, hiding widgets, popup menus, mouse position, mouse events.
 +
* [[PySide Advanced Examples]], many widgets.
  
Python offers the 'print' statement which gives the code:
+
The examples of PySide are divided into 3 parts, differentiated by level of exposure to PySide, Python and the FreeCAD internals. The first page has an overview on PySide; the second and third pages are mostly code examples at different levels.
{{Code|code=
 
print 'Hello World'
 
}}
 
With Python's print statement you have only limited control of the appearance and behaviour. PySide supplies the missing control and also handles environments (such as the FreeCAD macro file environment) where the built-in facilities of Python are not enough.
 
  
PySide's abilities range from:
+
It is expected that these examples are useful to get started, and afterwards the user can consult other resources online, or the official documentation.
  
[[File:PySideScreenSnapshot1.jpg]]
+
== Documentation ==
  
to:
+
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 in a given platform. Nevertheless, Qt4 is considered obsolete, so most development should target Qt5 and Python 3.
  
[[File:PySideScreenSnapshot2.jpg]]
+
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.
 +
* [https://doc.qt.io/qtforpython/modules.html Qt Modules] available from PySide2 (Qt5).
 +
* [https://doc.qt.io/qt-5/modules-cpp.html All Qt classes by module] in Qt5 for C++.
 +
* [https://deptinfo-ensip.univ-poitiers.fr/ENS/pyside-docs/index.html Qt Modules] available from PySide (Qt4).
  
'''Familiarize yourself with some real-world examples of PySide'''
+
{{Docnav
* [[PySide Beginner Examples]] (Hello World, announcements, enter text, enter number)
+
|[[Pivy|Pivy]]
* [[PySide Intermediate Examples]] (window sizing, hiding widgets, popup menus, mouse position, mouse events)
+
|[[FeaturePython Objects|FeaturePython Objects]]
* [[PySide Advanced Examples]] (widgets etc.)
+
}}
 
 
They divide the subject matter into 3 parts, differentiated by level of exposure to PySide, Python and the FreeCAD internals. The first page has overview and background material giving a description of PySide and how it is put together while the second and third pages are mostly code examples at different levels.
 
 
 
The intention is that the associated pages will provide simple Python code to run PySide so that the user working on a problem can easily copy the code, paste it into their own work, adapt it as necessary and return to their problem solving with FreeCAD. Hopefully they don't have to go chasing off across the internet looking for answers to PySide questions. At the same time this page is not intended to replace the various comprehensive PySide tutorials and reference sites available on the web.
 
 
 
{{docnav|Pivy|FeaturePython Objects}}
 
  
{{Userdocnavi{{#translation:}}}}
+
{{Powerdocnavi{{#translation:}}}}
[[Category:Poweruser Documentation{{#translation:}}]]
 
[[Category:Developer{{#translation:}}]]
 
 
[[Category:Developer Documentation{{#translation:}}]]
 
[[Category:Developer Documentation{{#translation:}}]]
 
[[Category:Python Code{{#translation:}}]]
 
[[Category:Python Code{{#translation:}}]]
 
{{clear}}
 
{{clear}}

Revision as of 20:20, 15 May 2020

Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Türkçe • ‎español • ‎français • ‎italiano • ‎polski • ‎português • ‎português do Brasil • ‎română • ‎svenska • ‎čeština • ‎русский • ‎日本語
Arrow-left.svg Previous: Pivy

Introduction

PySide is a library that allows you to use the cross-platform graphical user interface (GUI) toolkit Qt with Python. Qt is a C++ collection of libraries, so its components are normally available from C++, but with the help of PySide, we can use the same components from Python. This means that every complex graphical interface that we can image that can be created in C++, can also be created and modified in Python. An advantage of using Python is that Qt interfaces can be developed and tested live, as we don't need to compile the source files.

When you install FreeCAD, you should get both Qt and PySide as part of the package. If you are compiling yourself, then you must verify that these two libraries are installed, in order for FreeCAD to run correctly. Of course, PySide will only work if Qt is present.

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:

PySideScreenSnapshot1.jpg PySideScreenSnapshot2.jpg

Examples created with PySide. Left: a simple dialog. Right: a more complex dialog with graphs.

PySide in FreeCAD with 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

Real examples of PySide use

The examples of PySide are divided into 3 parts, differentiated by level of exposure to PySide, Python and the FreeCAD internals. The first page has an overview on PySide; the second and third pages are mostly code examples at different levels.

It is expected that these examples are useful to get started, and afterwards the user can consult other resources online, or the official documentation.

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

Arrow-left.svg Previous: Pivy