PySide/es: Difference between revisions

From FreeCAD Documentation
(Created page with "Si quieres trabajar en la interfaz de FreeCAD, lo primero que debes hacer es crear una referencia a la ventana principal de FreeCAD :")
(Created page with "A continuación, puedes navegar por ejemplo a través de todos los complementos (widgets) de la interfaz:")
Line 18: Line 18:
mw = app.activeWindow()
mw = app.activeWindow()
</syntaxhighlight>
</syntaxhighlight>
A continuación, puedes navegar por ejemplo a través de todos los complementos (widgets) de la interfaz:
Then, you can for example browse through all the widgets of the interface:
<syntaxhighlight>
<syntaxhighlight>
for child in mw.children():
for child in mw.children():

Revision as of 18:50, 17 October 2014

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 es un módulo de Python que permite a las aplicaciones en Python crear, acceder y modificar aplicaciones Qt. Se puede utilizar por ejemplo para crear tus propios programas de Qt en Python, o para acceder y modificar la interfaz de una aplicación Qt en ejecución, como FreeCAD.

Al utilizar el módulo de PyQt desde el interior de FreeCAD, se tiene un control total sobre su interfaz. Por ejemplo, puedes:

  • Añadir tus propios paneles, complementos (widgets) y barras de herramientas
  • Añadir u ocultar los elementos en los paneles existentes
  • Cambiar, redirigir o agregar conexiones entre todos los elementos

PyQt tiene una extensa documentación del API, y hay muchos tutoriales en la red que te enseñan cómo funciona.

Si quieres trabajar en la interfaz de FreeCAD, lo primero que debes hacer es crear una referencia a la ventana principal de FreeCAD :

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

A continuación, puedes navegar por ejemplo a través de todos los complementos (widgets) de la interfaz:

 for child in mw.children():
    print 'widget name = ', child.objectName(), ', widget type = ', child

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:

 myWidget = QtGui.QDockWidget()
 mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)

You could then add stuff directly to your 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

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 created graphically with the Qt Designer program. A typical object generated by Qt Designer is like this:

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

To use it, you just need to apply it to your freshly created widget like this:

 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