PySide/it: Difference between revisions

From FreeCAD Documentation
(Created page with "Per aggiungere un nuovo widget, ad esempio un dockWidget (che può essere posizionato in uno dei pannelli laterali di FreeCAD), fare semplicemente:")
(Updating to match new version of source page)
(48 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<languages/>
{{Note|PySide|Di recente, FreeCAD è stato modificato internamente per usare [http://qt-project.org/wiki/PySide PySide] al posto di PyQt. Questo cambiamento è stato fatto soprattutto per risolvere una questione di licenze, PySide ha una licenza LGPL che è più compatibile con FreeeCAD. A parte questo, PySide funziona esattamente come PyQt, e in genere in FreeCAD si può utilizzare indifferentemente uno dei due, come si preferisce. Se si decide di utilizzare PySide, basta sostituire tutti i "PyQt" con "PySide" nel codice di esempio sottostante. Vedere le
[http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differenze tra PySide e PyQt]}}


<div class="mw-translate-fuzzy">
[http://en.wikipedia.org/wiki/PyQt PyQt] è un modulo di Python che consente alle applicazioni in Python di creare, accedere e modificare le applicazioni [http://en.wikipedia.org/wiki/Qt_(toolkit) Qt]. Può essere utilizzato, per esempio, per creare i programmi Qt in Python oppure per accedere e modificare l'interfaccia di un'applicazione Qt in esecuzione, come FreeCAD.
{{docnav/it|[[Pivy/it|Pivy]]|[[FeaturePython Objects/it|Ogetti FeaturePython]]}}
</div>


{{TOCright}}
Quindi, usando il modulo PyQt all'interno di FreeCAD, si ha il controllo completo della sua interfaccia. È possibile ad esempio:
* Aggiungere propri pannelli, widget e barre degli strumenti
* Aggiungere o nascondere gli elementi nei pannelli già esistenti
* Modificare, reindirizzare o aggiungere connessioni tra tutti questi elementi


<div class="mw-translate-fuzzy">
PyQt ha una ampia [http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/classes.html documentazione API], e in rete ci sono molti tutorial che spiegano come funziona.
==PySide==
</div>


<div class="mw-translate-fuzzy">
Se si desidera lavorare sull'interfaccia di FreeCAD, la prima cosa da fare è creare un riferimento alla finestra principale di FreeCAD:
[http://en.wikipedia.org/wiki/PySide PySide] è il legame tra Python e la multi-piattaforma GUI degli strumenti di Qt. FreeCAD usa PySide all'interno di Python per tutti gli effetti GUI (Interfaccia grafica per l'utente). PySide è una alternativa al pacchetto PyQt che è stato utilizzato in precedenza da FreeCAD per la sua GUI. PySide ha una licenza più permissiva. Per maggiori informazioni sulla differenza vedere [http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences Between PySide and PyQt].
</div>

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==

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=
{{Code|code=
/usr/share/freecad/Ext/PySide
import sys
from PySide import QtGui ,QtCore
app = QtGui.qApp
mw = app.activeWindow()
}}
}}

In seguito, è possibile ad esempio sfogliare tutti i widget dell'interfaccia:
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
for child in mw.children():
PySide2.QtGui -> PySide.QtGui
print 'widget name = ', child.objectName(), ', widget type = ', child
PySide2.QtSvg -> PySide.QtSvg
PySide2.QtUiTools -> PySide.QtUiTools
}}
}}
Di solito, in una interfaccia Qt, i widget sono annidati in widget "contenitori", in questo modo i figli della finestra principale possono contenere altri figli. Secondo il tipo di widget, si possono fare un sacco di cose. Controllare la documentazione delle API per vedere quello che è possibile fare.


The only unusual aspect is that the {{incode|PySide2.QtWidgets}} classes are placed in the {{incode|PySide.QtGui}} namespace.
Per aggiungere un nuovo widget, ad esempio un dockWidget (che può essere posizionato in uno dei pannelli laterali di FreeCAD), fare semplicemente:
{{Code|code=
{{Code|code=
PySide2.QtWidgets.QCheckBox -> PySide.QtGui.QCheckBox
myWidget = QtGui.QDockWidget()
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)
}}
}}
You could then add stuff directly to your widget:
{{Code|code=
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
}}
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:
{{Code|code=
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


[[#top|top]]
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


==Examples of PySide use==
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))
}}
To use it, you just need to apply it to your freshly created widget like this:
{{Code|code=
app = QtGui.qApp
FCmw = app.activeWindow()
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
}}
{{docnav|Pivy|Scripted objects}}


<div class="mw-translate-fuzzy">
[[Category:Poweruser Documentation]]
'''Per acquisire familiarità con alcuni esempi reali di PySide'''
* [[PySide Beginner Examples/it|Esempi di livello base di PySide]] (Hello World, avvisi, inserire un testo, inserire un numero)
* [[PySide Intermediate Examples/it|Esempi di livello medio di PySide]] (dimensionare le finestre, nascondere i widget, i menu popup, la posizione del mouse, gli eventi del mouse)
* [[PySide Advanced Examples/it|Esempi di livello avanzato di PySide]] (widget etc.)
</div>


<div class="mw-translate-fuzzy">
Esse suddividono l'argomento in 3 parti, e si differenziano per livello di esposizione per PySide, Python e le parti incorporate in FreeCAD. La prima pagina contiene una panoramica e le basi della materia, fornisce una descrizione di PySide e di come è organizzato, mentre la seconda e la terza pagina contengono soprattutto degli esempi di codice di diversi livelli.
</div>

<div class="mw-translate-fuzzy">
Con le pagine allegate si intende fornire del semplice codice Python per eseguire PySide, in modo che l'utente possa facilmente copiarlo, incollarlo nel proprio lavoro, adattarlo se necessario, e risolvere i suoi problemi con FreeCAD. Sperando che non abbia bisogno di andare alla ricerca di risposte alle domande su PySide attraverso internet. Allo stesso tempo, queste pagine non vogliono sostituirsi ai vari tutorial completi e ai siti di riferimento a PySide presenti nel web.
</div>

[[#top|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.
* [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).

[[#top|top]]

<div class="mw-translate-fuzzy">
{{docnav/it|[[Pivy/it|Pivy]]|[[FeaturePython Objects/it|Ogetti FeaturePython]]}}
</div>

{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{clear}}
{{clear}}
<languages/>

Revision as of 22:08, 5 June 2020

PySide

PySide è il legame tra Python e la multi-piattaforma GUI degli strumenti di Qt. FreeCAD usa PySide all'interno di Python per tutti gli effetti GUI (Interfaccia grafica per l'utente). PySide è una alternativa al pacchetto PyQt che è stato utilizzato in precedenza da FreeCAD per la sua GUI. PySide ha una licenza più permissiva. Per maggiori informazioni sulla differenza vedere Differences Between PySide and PyQt.

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:

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

top

Examples of PySide use

Per acquisire familiarità con alcuni esempi reali di PySide

Esse suddividono l'argomento in 3 parti, e si differenziano per livello di esposizione per PySide, Python e le parti incorporate in FreeCAD. La prima pagina contiene una panoramica e le basi della materia, fornisce una descrizione di PySide e di come è organizzato, mentre la seconda e la terza pagina contengono soprattutto degli esempi di codice di diversi livelli.

Con le pagine allegate si intende fornire del semplice codice Python per eseguire PySide, in modo che l'utente possa facilmente copiarlo, incollarlo nel proprio lavoro, adattarlo se necessario, e risolvere i suoi problemi con FreeCAD. Sperando che non abbia bisogno di andare alla ricerca di risposte alle domande su PySide attraverso internet. Allo stesso tempo, queste pagine non vogliono sostituirsi ai vari tutorial completi e ai siti di riferimento a PySide presenti nel 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