PySide/en: Difference between revisions

From FreeCAD Documentation
(Updating to match new version of source page)
(Updating to match new version of source page)
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<languages/>
{{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". See [http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences Between PySide and PyQt]}}
{{docnav|Pivy|FeaturePython Objects}}


==PySide==
[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.


[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 Interface) purposes inside of Python. PySide is an alternative to the PyQt package which was previously used by FreeCAD for its GUI. PySide has a more permissible licence. See [http://qt-project.org/wiki/Differences_Between_PySide_and_PyQt Differences Between PySide and PyQt] for more information on the differences.
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


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 its user interaction Python with FreeCAD uses PySide, which is what is documented on this page.
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.


Python offers the 'print' statement which gives the code:
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=
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.
Then, you can for example browse through all the widgets of the interface:
{{Code|code=
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.


PySide's abilities range from:
Adding a new widget, for example a dockWidget (which can be placed in one of FreeCAD's side panels) is easy:
{{Code|code=
myWidget = QtGui.QDockWidget()
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)
}}
You could then add stuff directly to your 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
}}
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:
{{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))

}}
'''Familiarize yourself with some real-world examples of PySide'''
To use it, you just need to apply it to your freshly created widget like this:
* [[PySide Beginner Examples]] (Hello World, announcements, enter text, enter number)
{{Code|code=
* [[PySide Intermediate Examples]] (window sizing, hiding widgets, popup menus, mouse position, mouse events)
app = QtGui.qApp
* [[PySide Advanced 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

}}
{{docnav|Pivy|Scripted objects}}
{{docnav|Pivy|FeaturePython Objects}}

{{Userdocnavi}}


[[Category:Poweruser Documentation]]
[[Category:Poweruser Documentation]]


[[Category:Developer]]

[[Category:Developer Documentation]]
{{clear}}
{{clear}}
<languages/>

Revision as of 10:27, 26 July 2019

Pivy
FeaturePython Objects

PySide

PySide is a Python binding of the cross-platform GUI toolkit Qt. FreeCAD uses PySide for all GUI (Graphic User Interface) purposes inside of Python. PySide is an alternative to the PyQt package which was previously used by FreeCAD for its GUI. PySide has a more permissible licence. 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 its 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:

Familiarize yourself with some real-world examples of PySide

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.

Pivy
FeaturePython Objects