Sandbox:Piffpoof2

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:

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  

PySide is described in the following 3 pages which should follow on one from each other.


 * Entry Level PySide Examples (Hello World, announcements, enter text, enter number)
 * Intermediate 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 idea is that the associated pages will provide simple Python to run PySide so that the user working on a problem can easily copy the code, change the values of some variables (e.g. the text string with the text of the question) and paste in the code 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 no intended to replace the comprehensive PySide tutorials available on the web.

"========================="



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:

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

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:

You could then add stuff directly to your widget:

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:

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