Difference between revisions of "PySide/ro"

From FreeCAD Documentation
Jump to navigation Jump to search
(Updating to match new version of source page)
 
(41 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Note|PySide|Recently, FreeCAD has switched internally to use [http://qt-project.org/wiki/PySide PySide] instead of PyQt. That change was mainly done because of the licenses, PySide having an LGPL license which is more compatible with FreeeCAD. Other than that, PySide works exactly the same way as PyQt, and in FreeCAD you can usually use any of them, as you prefer. If you choose to use PySide, just replace all "PyQt" in the example code below with "PySide".<br />
+
<languages/>
[http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences Between PySide and PyQt]}}
+
 
 +
{{Docnav
 +
|[[Pivy|Pivy]]
 +
|[[Interface_creation|Interface creation]]
 +
}}
 +
 
 +
{{TOCright}}
 +
 
 +
<div class="mw-translate-fuzzy">
 +
<H2>PySide</H2>
 +
</div>
 +
 
 +
<div class="mw-translate-fuzzy">
 +
[http://en.wikipedia.org/wiki/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 [http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences Between PySide and PyQt] for more information on the differences.
 +
</div>
 +
 
 +
When you 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 commit 1dc122dc9a]) the project migrated to PySide because it has a more permissible [[licence|license]].
 +
 
 +
For more information see:
 +
* [https://en.wikipedia.org/wiki/PySide Wikipedia:PySide]
 +
* [https://wiki.qt.io/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==
 +
 
 +
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 {{incode|PySide}} module provides support for Qt4, while {{incode|PySide2}} provides support for Qt5. However, in FreeCAD there is no need to use {{incode|PySide2}} directly, as a special {{incode|PySide}} module is included to handle Qt5.
 +
 
 +
This {{incode|PySide}} module is located in the {{incode|Ext/}} directory of an installation of FreeCAD compiled for Qt5.
 +
{{Code|code=
 +
/usr/share/freecad/Ext/PySide
 +
}}
 +
 
 +
This module just imports the necessary classes from {{incode|PySide2}}, and 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 we use the single {{incode|PySide}} module.
 +
{{Code|code=
 +
PySide2.QtCore -> PySide.QtCore
 +
PySide2.QtGui -> PySide.QtGui
 +
PySide2.QtSvg -> PySide.QtSvg
 +
PySide2.QtUiTools -> PySide.QtUiTools
 +
}}
 +
 
 +
The only unusual aspect is that the {{incode|PySide2.QtWidgets}} classes are placed in the {{incode|PySide.QtGui}} namespace.
 +
{{Code|code=
 +
PySide2.QtWidgets.QCheckBox -> PySide.QtGui.QCheckBox
 +
}}
 +
{{Top}}
 +
==Examples of PySide use==
  
[http://en.wikipedia.org/wiki/PyQt PyQt] is a python module that allows python applications to create, access and modify [http://en.wikipedia.org/wiki/Qt_(toolkit) Qt] applications. You can use it for example to create your own Qt programs in python, or to access and modify the interface of a running qt application, like FreeCAD.  
+
<div class="mw-translate-fuzzy">
 +
* [[PySide_Beginner_Examples|Beginner PySide Examples]] (Hello World, anunțuri, introducerea textului, intgroducerea numerului)
 +
* [[PySide_Intermediate_Examples|PySide Intermediate Examples]] (mărimea ferestrei, ascunderea widgets, meniuri popup, poziția mouse , evenimentele mouse-ului)
 +
* [[PySide_Advanced_Examples|Advanced PySide Examples]] (widgets etc.)
 +
</div>
  
By using the PyQt module from inside FreeCAD, you have therefore full control over its interface. You can for example:
+
<div class="mw-translate-fuzzy">
* Add your own panels, widgets and toolbars
+
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.
* Add or hide elements to existing panels
+
</div>
* Change, redirect or add connections between all those elements
 
  
PyQt has an extensive [http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/classes.html API documentation], and there are many tutorials on the net to teach you how it works.
+
<div class="mw-translate-fuzzy">
 +
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.
 +
</div>
 +
{{Top}}
 +
==Documentation==
  
If you want to work on the FreeCAD interface, the very first thing to do is create a reference to the FreeCAD main window:
+
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.
<syntaxhighlight>
 
import sys
 
from PyQt4 import QtGui
 
app = QtGui.qApp
 
mw = app.activeWindow()
 
</syntaxhighlight>
 
Then, you can for example browse through all the widgets of the interface:
 
<syntaxhighlight>
 
for child in mw.children():
 
    print 'widget name = ', child.objectName(), ', widget type = ', child
 
</syntaxhighlight>
 
The widgets in a Qt interface are usually nested into "containers" widgets, so the children of our main window can themselves contain other children. Depending on the widget type, there are a lot of things you can do. Check the API documentation to see what is possible.
 
  
Adding a new widget, for example a dockWidget (which can be placed in one of FreeCAD's side panels) is easy:
+
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.
<syntaxhighlight>
+
* [https://doc.qt.io/qtforpython/modules.html Qt Modules] available from PySide2 (Qt5).
myWidget = QtGui.QDockWidget()
+
* [https://doc.qt.io/qt-5/modules-cpp.html All Qt classes by module] in Qt5 for C++.
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)
+
* [https://deptinfo-ensip.univ-poitiers.fr/ENS/pyside-docs/index.html Qt Modules] available from PySide (Qt4).
</syntaxhighlight>
+
{{Top}}
You could then add stuff directly to your widget:
 
<syntaxhighlight>
 
  myWidget.setObjectName("my Nice New Widget")
 
  myWidget.resize(QtCore.QSize(300,100)) # sets size of the widget
 
  label = QtGui.QLabel("Hello World", myWidget) # creates a label
 
  label.setGeometry(QtCore.QRect(50,50,200,24)) # sets its size
 
  label.setObjectName("myLabel") # sets its name, so it can be found by name
 
</syntaxhighlight>
 
But a preferred method is to create a UI object which will do all of the setup of your widget at once. The big advantage is that such an UI object can be [[Dialog creation|created graphically]] with the Qt Designer program. A typical object generated by Qt Designer is like this:
 
<syntaxhighlight>
 
class myWidget_Ui(object):
 
  def setupUi(self, myWidget):
 
    myWidget.setObjectName("my Nice New Widget")
 
    myWidget.resize(QtCore.QSize(300,100).expandedTo(myWidget.minimumSizeHint())) # sets size of the widget
 
 
    self.label = QtGui.QLabel(myWidget) # creates a label
 
    self.label.setGeometry(QtCore.QRect(50,50,200,24)) # sets its size
 
    self.label.setObjectName("label") # sets its name, so it can be found by name
 
 
  def retranslateUi(self, draftToolbar): # built-in QT function that manages translations of widgets
 
    myWidget.setWindowTitle(QtGui.QApplication.translate("myWidget", "My Widget", None, QtGui.QApplication.UnicodeUTF8))
 
    self.label.setText(QtGui.QApplication.translate("myWidget", "Welcome to my new widget!", None, QtGui.QApplication.UnicodeUTF8))
 
</syntaxhighlight>
 
To use it, you just need to apply it to your freshly created widget like this:
 
<syntaxhighlight>
 
myNewFreeCADWidget = QtGui.QDockWidget() # create a new dckwidget
 
myNewFreeCADWidget.ui = myWidget_Ui() # load the Ui script
 
myNewFreeCADWidget.ui.setupUi(myNewFreeCADWidget) # setup the ui
 
FCmw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myNewFreeCADWidget) # add the widget to the main window
 
</syntaxhighlight>
 
{{docnav/ro|Pivy/ro|Scripted objects/ro}}
 
  
[[Category:Poweruser Documentation/ro]]
+
<div class="mw-translate-fuzzy">
 +
{{docnav|Pivy|Scripted objects}}
 +
</div>
  
{{clear}}
+
{{Powerdocnavi{{#translation:}}}}
<languages/>
+
[[Category:Developer Documentation{{#translation:}}]]
 +
[[Category:Python Code{{#translation:}}]]

Latest revision as of 14:51, 13 January 2022

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

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.

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 (commit 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, and 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 we use 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

Top

Examples of PySide use

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.

Top

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.

Top