PySide/fr: Difference between revisions

From FreeCAD Documentation
(Updating to match new version of source page)
Line 1: Line 1:
{{Note|PySide|Depuis peu, FreeCAD travaille avec [http://qt-project.org/wiki/PySide PySide] au lieu de PyQt. Ce changement a été principalement fait pour adapter la licence, PySide ayant une licence LGPL qui est plus compatible avec FreeeCAD. A part cela, PySide fonctionne exactement de la même manière que PyQt, et dans FreeCAD vous pouvez généralement utiliser l'un ou l'autre, comme vous désirez. Si vous choisissez d'utiliser PySide, il suffit de remplacer tous les "PyQt" dans les exemples de code ci-dessous avec "PySide".
{{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".


[http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences entren PySide et PyQt]}}
[http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences Between PySide and PyQt]}}


[http://fr.wikipedia.org/wiki/PyQt PyQt] est un module python qui permet aux applications python de créer, d'accéder et de modifier les applications '''[http://fr.wikipedia.org/wiki/Qt Qt]'''.<br />
[http://en.wikipedia.org/wiki/PyQt PyQt] is a python module that allows python applications to create, access and modify [http://en.wikipedia.org/wiki/Qt_(toolkit) Qt] applications. You can use it for example to create your own Qt programs in python, or to access and modify the interface of a running qt application, like FreeCAD.
Vous pouvez l'utiliser par exemple:<br />
* pour créer vos propres programmes Qt en python,<br />
* ou pour accéder et modifier l'interface d'une application qt en cours d'exécution, comme FreeCAD.


En utilisant le module PyQt dans FreeCAD, vous avez le contrôle total de son interface.<br />
By using the PyQt module from inside FreeCAD, you have therefore full control over its interface. You can for example:
* Add your own panels, widgets and toolbars
Vous pouvez par exemple:<br />
* Add or hide elements to existing panels
* Ajouter vos propres fenêtres, des widgets et des barres d'outils.<br />
* Change, redirect or add connections between all those elements
* Ajouter ou masquer des éléments des fenêtres existantes.<br />
* Changer, rediriger ou ajouter des connexions entre tous ces éléments.


PyQt dispose d'une vaste documentation sur [http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/classes.html son API] , et il a de nombreux tutoriels sur le net pour vous en apprendre le fonctionnement.
PyQt has an extensive [http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/classes.html API documentation], and there are many tutorials on the net to teach you how it works.


If you want to work on the FreeCAD interface, the very first thing to do is create a reference to the FreeCAD main window:
Si vous voulez travailler sur l'interface de FreeCAD, la première chose à faire est de créer une référence à la fenêtre principale de FreeCAD.<br />
{{Code|code=
Faisons:
import sys
<syntaxhighlight>
import sys
from PySide import QtGui ,QtCore
from PyQt4 import QtGui
app = QtGui.qApp
mw = app.activeWindow()
app = QtGui.qApp
}}
mw = app.activeWindow()
Then, you can for example browse through all the widgets of the interface:
</syntaxhighlight>
{{Code|code=
Ensuite, vous pouvez par exemple naviguer à travers tous les widgets de l'interface:
for child in mw.children():
<syntaxhighlight>
print 'widget name = ', child.objectName(), ', widget type = ', child
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.
</syntaxhighlight>
Les widgets d'une interface Qt sont généralement imbriqués dans des "conteneurs" widgets, de sorte que les enfants de notre fenêtre principale peuvent contenir d'autres enfants. Selon le type de widget, vous pouvez faire énormément de choses.<br />
Vérifiez la documentation de l'API pour voir ce qui est possible.


Ajout d'un nouveau widget, par exemple un dockWidget (qui peut être placé dans l'un des panneaux latéraux de FreeCAD) est facile:
Adding a new widget, for example a dockWidget (which can be placed in one of FreeCAD's side panels) is easy:
{{Code|code=
<syntaxhighlight>
myWidget = QtGui.QDockWidget()
myWidget = QtGui.QDockWidget()
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)
}}
</syntaxhighlight>
You could then add stuff directly to your widget:
Vous pouvez ensuite ajouter ce que vous voulez directement sur votre widget:
{{Code|code=
<syntaxhighlight>
myWidget.setObjectName("my Nice New Widget")
myWidget.setObjectName("my Nice New Widget")
myWidget.resize(QtCore.QSize(300,100)) # sets size of the widget
myWidget.resize(QtCore.QSize(300,100)) # sets size of the widget
label = QtGui.QLabel("Hello World", myWidget) # creates a label
label = QtGui.QLabel("Hello World", myWidget) # creates a label
label.setGeometry(QtCore.QRect(50,50,200,24)) # sets its size
label.setGeometry(QtCore.QRect(50,50,200,24)) # sets its size
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
}}
</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:
Mais la meilleure méthode est de créer un objet "interface utilisateur" (UI) qui fera toute la configuration de votre widget.<br />
{{Code|code=
Le gros avantage est, qu'une telle interface utilisateur peut être créées graphiquement avec le programme Qt Designer.<br />
class myWidget_Ui(object):
Un objet typique généré par Qt Designer est fait comme ceci:
def setupUi(self, myWidget):
<syntaxhighlight>
myWidget.setObjectName("my Nice New Widget")
class myWidget_Ui(object):
myWidget.resize(QtCore.QSize(300,100).expandedTo(myWidget.minimumSizeHint())) # sets size of the widget
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>
Pour l'utiliser, il suffit de l'appliquer à votre widget fraîchement créé,<br />
comme ceci:
<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/fr|[[Pivy/fr|Pivy]]|[[Scripted objects/fr|Scripted objects]]}}


self.label = QtGui.QLabel(myWidget) # creates a label
[[Category:Poweruser Documentation/fr]]
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:
{{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}}

[[Category:Poweruser Documentation]]


{{clear}}
{{clear}}

Revision as of 21:26, 22 December 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 is a python module that allows python applications to create, access and modify Qt applications. You can use it for example to create your own Qt programs in python, or to access and modify the interface of a running qt application, like FreeCAD.

By using the PyQt module from inside FreeCAD, you have therefore full control over its interface. You can for example:

  • Add your own panels, widgets and toolbars
  • Add or hide elements to existing panels
  • Change, redirect or add connections between all those elements

PyQt has an extensive API documentation, and there are many tutorials on the net to teach you how it works.

If you want to work on the FreeCAD interface, the very first thing to do is create a reference to the FreeCAD main window:

import sys
from PySide import QtGui ,QtCore 
app = QtGui.qApp
mw = app.activeWindow()

Then, you can for example browse through all the widgets of the interface:

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:

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