PySide

From FreeCAD Documentation
Revision as of 20:29, 16 October 2014 by Renatorivo (talk | contribs) (Created page with "Per usarlo, basta applicarlo al pannello (widget) appena creato in questo modo:")
PySide

Recently, FreeCAD has switched internally to use 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".
Differences Between PySide and PyQt


PyQt è un modulo di Python che consente alle applicazioni in Python di creare, accedere e modificare le applicazioni 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.

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

PyQt ha una ampia documentazione API, e in rete ci sono molti tutorial che spiegano come funziona.

Se si desidera lavorare sull'interfaccia di FreeCAD, la prima cosa da fare è creare un riferimento alla finestra principale di FreeCAD:

 import sys
 from PyQt4 import QtGui
 app = QtGui.qApp
 mw = app.activeWindow()

In seguito, è possibile ad esempio sfogliare 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.

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(50,50,200,24)) # sets its size
   label.setObjectName("myLabel") # sets its name, so it can be found by name

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:

 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
Pivy
Scripted objects