PySide/it: Difference between revisions

From FreeCAD Documentation
(Created page with "In seguito, si può continuare e aggiungere altre cose direttamente al proprio widget:")
(Updating to match new version of source page)
(66 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<languages/>
{{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 />
[http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences Between PySide and PyQt]}}


{{TOCright}}
[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.


<div class="mw-translate-fuzzy">
Quindi, usando il modulo PyQt all'interno di FreeCAD, si ha il controllo completo della sua interfaccia. È possibile ad esempio:
==PySide==
* Aggiungere propri pannelli, widget e barre degli strumenti
</div>
* 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.
[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.
Se si desidera lavorare sull'interfaccia di FreeCAD, la prima cosa da fare è creare un riferimento alla finestra principale di FreeCAD:
<syntaxhighlight>
import sys
from PyQt4 import QtGui
app = QtGui.qApp
mw = app.activeWindow()
</syntaxhighlight>
In seguito, è possibile ad esempio sfogliare tutti i widget dell'interfaccia:
<syntaxhighlight>
for child in mw.children():
print 'widget name = ', child.objectName(), ', widget type = ', child
</syntaxhighlight>
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.


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]].
Per aggiungere un nuovo widget, ad esempio un dockWidget (che può essere posizionato in uno dei pannelli laterali di FreeCAD), fare semplicemente:
<syntaxhighlight>
myWidget = QtGui.QDockWidget()
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)
</syntaxhighlight>
In seguito, si può continuare e aggiungere altre cose direttamente al proprio 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|Pivy|Scripted objects}}


For more information see:
[[Category:Poweruser Documentation]]
* [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=
/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|top]]

==Examples of PySide use==

<div class="mw-translate-fuzzy">
'''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]]

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

Revision as of 10:20, 3 October 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, 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

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