PySide/fr: Difference between revisions
(Updating to match new version of source page) |
|||
Line 1: | Line 1: | ||
<H2>PySide</H2> |
|||
{{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". |
|||
[http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences |
[http://en.wikipedia.org/wiki/PySide PySide] is a Python binding of the cross-platform GUI toolkit Qt. FreeCAD uses PySide for all GUI (Graphic User Intercase) purposes. PySide evolved from the PyQt package which was previously used by FreeCAD for it's GUI. See [http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences Between PySide and PyQt] for more information on the differences. |
||
Users of FreeCAD often achieve everything using the built-in interface. But for users who want to customise their operations then the Python interface exists which is documented in the [[Python_scripting_tutorial|Python Scripting Tutorial]]. The Python interface for FreeCAD had great flexibility and power. For it's user interaction Python with FreeCAD uses PySide, which is what is documented on this page. |
|||
[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 /> |
|||
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. |
|||
Python offers the 'print' statement which gives the code: |
|||
En utilisant le module PyQt dans FreeCAD, vous avez le contrôle total de son interface. |
|||
Vous pouvez par exemple: |
|||
* Ajouter vos propres fenêtres, des widgets et des barres d'outils. |
|||
* Ajouter ou masquer des éléments des fenêtres existantes. |
|||
* 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. |
|||
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 /> |
|||
Faisons: |
|||
{{Code|code= |
{{Code|code= |
||
print 'Hello World' |
|||
import sys |
|||
from PySide import QtGui ,QtCore |
|||
app = QtGui.qApp |
|||
mw = FreeCADGui.getMainWindow() |
|||
}} |
}} |
||
With Python's print statement you have only limited control of the appearance and behaviour. PySide supplies the missing control and also handles environments (such as the FreeCAD macro file environment) where the built-in facilities of Python are not enough. |
|||
Ensuite, vous pouvez par exemple naviguer à travers tous les widgets de l'interface: |
|||
{{Code|code= |
|||
for child in mw.children(): |
|||
print 'widget name = ', child.objectName(), ', widget type = ', child |
|||
}} |
|||
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. |
|||
PySide's abilities range from: |
|||
Ajout d'un nouveau widget, par exemple un dockWidget (qui peut être placé dans l'un des panneaux latéraux de FreeCAD) est facile: |
|||
{{Code|code= |
|||
myWidget = QtGui.QDockWidget() |
|||
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget) |
|||
}} |
|||
Vous pouvez ensuite ajouter ce que vous voulez directement sur votre widget: |
|||
{{Code|code= |
|||
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 |
|||
}} |
|||
Mais la meilleure méthode est de créer un objet "interface utilisateur" (UI) qui fera toute la configuration de votre widget. |
|||
Le gros avantage est, qu'une telle interface utilisateur peut être créées graphiquement avec le programme Qt Designer. |
|||
Un objet typique généré par Qt Designer est fait comme ceci: |
|||
{{Code|code= |
|||
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 |
|||
[[File:PySideScreenSnapshot1.jpg]] |
|||
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 |
|||
to: |
|||
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)) |
|||
[[File:PySideScreenSnapshot2.jpg]] |
|||
self.label.setText(QtGui.QApplication.translate("myWidget", "Welcome to my new widget!", None, QtGui.QApplication.UnicodeUTF8)) |
|||
}} |
|||
PySide is described in the following 3 pages which should follow on one from each other: |
|||
Pour l'utiliser, il suffit de l'appliquer à votre widget fraîchement créé, |
|||
comme ceci: |
|||
* [[PySide_Beginner_Examples|Beginner PySide Examples]] (Hello World, announcements, enter text, enter number) |
|||
{{Code|code= |
|||
* [[PySide_Medium_Examples|Medium PySide Examples]] (window sizing, hiding widgets, popup menus, mouse position, mouse events) |
|||
app = QtGui.qApp |
|||
* [[PySide_Advanced_Examples|Advanced PySide Examples]] (widgets etc.) |
|||
FCmw = app.activeWindow() |
|||
myNewFreeCADWidget = QtGui.QDockWidget() # create a new dckwidget |
|||
They divide the subject matter into 3 parts, differentiated by level of exposure to PySide, Python and the FreeCAD internals. The first page has overview and background material giving a description of PySide and how it is put together while the second and third pages are mostly code examples at different levels. |
|||
myNewFreeCADWidget.ui = myWidget_Ui() # load the Ui script |
|||
myNewFreeCADWidget.ui.setupUi(myNewFreeCADWidget) # setup the ui |
|||
The intention is that the associated pages will provide simple Python code to run PySide so that the user working on a problem can easily copy the code, paste it into their own work, adapt it as necessary and return to their problem solving with FreeCAD. Hopefully they don't have to go chasing off across the internet looking for answers to PySide questions. At the same time this page is not intended to replace the various comprehensive PySide tutorials and reference sites available on the web. |
|||
FCmw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myNewFreeCADWidget) # add the widget to the main window |
|||
}} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{{clear}} |
{{clear}} |
||
<languages/> |
<languages/> |
Revision as of 18:17, 9 February 2015
PySide
PySide is a Python binding of the cross-platform GUI toolkit Qt. FreeCAD uses PySide for all GUI (Graphic User Intercase) purposes. PySide evolved from the PyQt package which was previously used by FreeCAD for it's GUI. See Differences Between PySide and PyQt for more information on the differences.
Users of FreeCAD often achieve everything using the built-in interface. But for users who want to customise their operations then the Python interface exists which is documented in the Python Scripting Tutorial. The Python interface for FreeCAD had great flexibility and power. For it's user interaction Python with FreeCAD uses PySide, which is what is documented on this page.
Python offers the 'print' statement which gives the code:
print 'Hello World'
With Python's print statement you have only limited control of the appearance and behaviour. PySide supplies the missing control and also handles environments (such as the FreeCAD macro file environment) where the built-in facilities of Python are not enough.
PySide's abilities range from:
to:
PySide is described in the following 3 pages which should follow on one from each other:
- Beginner PySide Examples (Hello World, announcements, enter text, enter number)
- Medium PySide Examples (window sizing, hiding widgets, popup menus, mouse position, mouse events)
- Advanced PySide Examples (widgets etc.)
They divide the subject matter into 3 parts, differentiated by level of exposure to PySide, Python and the FreeCAD internals. The first page has overview and background material giving a description of PySide and how it is put together while the second and third pages are mostly code examples at different levels.
The intention is that the associated pages will provide simple Python code to run PySide so that the user working on a problem can easily copy the code, paste it into their own work, adapt it as necessary and return to their problem solving with FreeCAD. Hopefully they don't have to go chasing off across the internet looking for answers to PySide questions. At the same time this page is not intended to replace the various comprehensive PySide tutorials and reference sites available on the web.