PySide Advanced Examples/it: Difference between revisions
Renatorivo (talk | contribs) (Created page with "==Introduzione== Questa pagina contiene degli esempi di livello avanzato di gestione della GUI con PySide. Gli [[PySide_Beginner_Examples/it|Esempi di base di Py...") |
(Updating to match new version of source page) |
||
(25 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
⚫ | |||
{{TOCright}} |
|||
<span id="Introduction"></span> |
|||
==Introduzione== |
==Introduzione== |
||
⚫ | |||
<div class="mw-translate-fuzzy"> |
|||
By using the PySide module from inside FreeCAD, you have full control over its interface. You can for example: |
|||
⚫ | |||
* Add your own panels, widgets and toolbars |
|||
</div> |
|||
* Add or hide elements to existing panels |
|||
* Change, redirect or add connections between all those elements |
|||
Quindi, usando il modulo PySide 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"> |
|||
==Creare un riferimento per la finestra principale== |
|||
Se si vuole lavorare sull'interfaccia di FreeCAD, la prima cosa da fare è creare un riferimento alla finestra principale di FreeCAD: |
|||
</div> |
|||
⚫ | |||
If you want to work on the FreeCAD interface, the very first thing to do is create a reference to the FreeCAD main window: |
|||
{{Code|code= |
{{Code|code= |
||
import sys |
import sys |
||
Line 15: | Line 28: | ||
mw = FreeCADGui.getMainWindow() |
mw = FreeCADGui.getMainWindow() |
||
}} |
}} |
||
==Browse the Children of the Main Window== |
==Browse the Children of the Main Window== |
||
Then, you can for example browse through all the widgets of the interface: |
|||
<div class="mw-translate-fuzzy"> |
|||
==Esplorare gli elementi della finestra principale== |
|||
Quindi, è possibile ad esempio esplorare tutti i widget dell'interfaccia: |
|||
</div> |
|||
{{Code|code= |
{{Code|code= |
||
for child in mw.children(): |
for child in mw.children(): |
||
print |
print('widget name = ', child.objectName(), ', widget type = ', child) |
||
}} |
}} |
||
The widgets in a Qt interface are usually nested into "container" 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. |
|||
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. |
|||
==Add New Widget Manually== |
==Add New Widget Manually== |
||
Adding a new widget, for example a dockWidget (which can be placed in one of FreeCAD's side panels) is easy: |
|||
<div class="mw-translate-fuzzy"> |
|||
==Aggiungere manualmente un nuovo widget== |
|||
Per aggiungere un nuovo widget, ad esempio un dockWidget (che può essere posizionato in uno dei pannelli laterali di FreeCAD), fare semplicemente: |
|||
</div> |
|||
{{Code|code= |
{{Code|code= |
||
myWidget = QtGui.QDockWidget() |
myWidget = QtGui.QDockWidget() |
||
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget) |
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget) |
||
}} |
}} |
||
You could then add stuff directly to your widget: |
|||
In seguito, si può continuare e aggiungere altre cose direttamente al proprio widget: |
|||
{{Code|code= |
{{Code|code= |
||
myWidget.setObjectName("my Nice New Widget") |
myWidget.setObjectName("my Nice New Widget") |
||
Line 37: | Line 64: | ||
label.setObjectName("myLabel") # sets its name, so it can be found by name |
label.setObjectName("myLabel") # sets its name, so it can be found by name |
||
}} |
}} |
||
==Add New Widget by Creating UI Object== |
==Add New Widget by Creating UI Object== |
||
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: |
|||
<div class="mw-translate-fuzzy"> |
|||
==Aggiungere un nuovo widget creando un oggetto UI== |
|||
Di solito, il metodo preferito consiste nel creare un oggetto UI (interfaccia utente) che faccia tutta la configurazione del proprio widget in una sola volta. Il grande vantaggio è che tale oggetto dell'interfaccia utente può essere [[Dialog creation/it|creato graficamente]] con il programma Qt Designer. Un tipico oggetto generato da Qt Designer si presenta come questo: |
|||
</div> |
|||
{{Code|code= |
{{Code|code= |
||
class myWidget_Ui(object): |
class myWidget_Ui(object): |
||
Line 53: | Line 86: | ||
self.label.setText(QtGui.QApplication.translate("myWidget", "Welcome to my new 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: |
|||
Per usarlo, basta applicarlo al pannello (widget) appena creato in questo modo: |
|||
{{Code|code= |
{{Code|code= |
||
app = QtGui.qApp |
app = QtGui.qApp |
||
Line 63: | Line 98: | ||
}} |
}} |
||
==Loading the UI from a Qt Designer .ui File== |
|||
⚫ | |||
The key to loading a UI file successfully is to use the full path to the file. As an example, the [[Std_AddonMgr|Addon Manager]] does it like this: |
|||
{{Code|code= |
|||
self.dialog = FreeCADGui.PySideUic.loadUi(os.path.join(os.path.dirname(__file__), "AddonManager.ui")) |
|||
}} |
|||
{{Powerdocnavi{{#translation:}}}} |
|||
⚫ | |||
[[Category:Python Code{{#translation:}}]] |
|||
{{clear}} |
{{clear}} |
||
⚫ |
Latest revision as of 14:02, 26 February 2023
Introduzione
Questa pagina contiene degli esempi di livello avanzato di gestione della GUI con PySide. Gli Esempi di base di PySide e gli Esempi di livello medio di PySide sono contenuti nelle rispettive pagine.
Quindi, usando il modulo PySide 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
Create Reference to the Main Window
Creare un riferimento per la finestra principale
Se si vuole lavorare sull'interfaccia di FreeCAD, la prima cosa da fare è creare un riferimento alla finestra principale di FreeCAD:
import sys
from PySide import QtGui ,QtCore
app = QtGui.qApp
mw = FreeCADGui.getMainWindow()
Browse the Children of the Main Window
Esplorare gli elementi della finestra principale
Quindi, è possibile ad esempio esplorare tutti i widget dell'interfaccia:
for child in mw.children():
print('widget name = ', child.objectName(), ', widget type = ', child)
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.
Add New Widget Manually
Aggiungere manualmente un nuovo widget
Per aggiungere un nuovo widget, ad esempio un dockWidget (che può essere posizionato in uno dei pannelli laterali di FreeCAD), fare semplicemente:
myWidget = QtGui.QDockWidget()
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)
In seguito, si può continuare e aggiungere altre cose direttamente al proprio widget:
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(2,50,200,24)) # sets its size
label.setObjectName("myLabel") # sets its name, so it can be found by name
Add New Widget by Creating UI Object
Aggiungere un nuovo widget creando un oggetto UI
Di solito, il metodo preferito consiste nel creare un oggetto UI (interfaccia utente) che faccia tutta la configurazione del proprio widget in una sola volta. Il grande vantaggio è che tale oggetto dell'interfaccia utente può essere creato graficamente con il programma Qt Designer. Un tipico oggetto generato da Qt Designer si presenta come questo:
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))
Per usarlo, basta applicarlo al pannello (widget) appena creato in questo modo:
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
Loading the UI from a Qt Designer .ui File
The key to loading a UI file successfully is to use the full path to the file. As an example, the Addon Manager does it like this:
self.dialog = FreeCADGui.PySideUic.loadUi(os.path.join(os.path.dirname(__file__), "AddonManager.ui"))
- 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