Interface creation: Difference between revisions

From FreeCAD Documentation
(→‎Introduction: File:FreeCAD_creating_interfaces.svg, 600px)
(Removed Interface_creation_with_UI_files link.)
 
(6 intermediate revisions by 3 users not shown)
Line 2: Line 2:
<translate>
<translate>


<!--T:13-->
== Introduction ==
{{Docnav
|[[PySide|PySide]]
|[[Dialog_creation|Dialog creation]]
}}


</translate>
Power users have the possibility of [[Interface creation|creating interfaces]] to help them produce complex tools for their custom [[Addon|addons]], such as [[macros|macros]] or full [[workbench|workbenches]].
{{TOCright}}
<translate>

== Introduction == <!--T:1-->

<!--T:2-->
Power users have the possibility of [[Interface_creation|creating interfaces]] to help them produce complex tools for their custom [[Addon|addons]], such as [[Macros|macros]] or full [[Workbenches|workbenches]].


<!--T:3-->
Interfaces are created using [[PySide|PySide]], which is a library for using Qt with [[Python|Python]].
Interfaces are created using [[PySide|PySide]], which is a library for using Qt with [[Python|Python]].


<!--T:4-->
[[File:FreeCAD_creating_interfaces.svg|600px]]
[[File:FreeCAD_creating_interfaces.svg|600px]]
{{Caption|Two general methods to create interfaces, by including the interface in the Python file, or by using {{incode|.ui}} files.}}
{{Caption|Two general methods to create interfaces, by including the interface in the Python file, or by using {{incode|.ui}} files.}}


== Description ==
== Description == <!--T:5-->


<!--T:6-->
There are typically two ways of creating interfaces with PySide.
There are typically two ways of creating interfaces with PySide.


=== Interface in a .ui file ===
=== Interface in a .ui file === <!--T:7-->


<!--T:8-->
In this method the interface is defined in a {{incode|.ui}} file (an XML document that defines the structure of the interface), which is then imported into [[Python|Python]] code that uses it. This is the recommended approach.
In this method the interface is defined in a {{incode|.ui}} file (an XML document that defines the structure of the interface), which is then imported into [[Python|Python]] code that uses it. This is the recommended approach.
* It allows the programmer to work with the graphical interface separately from the logic that will use it.
* It allows the programmer to work with the graphical interface separately from the logic that will use it.
* It allows anybody to look at the interface alone, that is, the {{incode|.ui}} file, without having to run Python code.
* It allows anybody to look at the interface alone, that is, the {{incode|.ui}} file, without having to run Python code.
* The {{incode|.ui}} file may be designed by anybody without programming knowledge.
* The {{incode|.ui}} file may be designed by anybody without programming knowledge.
* The {{incode|.ui}} interface can be used in a standalone window (modal), or in an embedded window (non-modal); therefore, this method is ideal to create custom [[task_panel|task panels]].
* The {{incode|.ui}} interface can be used in a standalone window (modal), or in an embedded window (non-modal); therefore, this method is ideal to create custom [[Task_panel|task panels]].
* Since the {{incode|.ui}} file just describes the "appearance" of the interface, it does not need to be tied to a particular programming language; it may be used both in [[Python|Python]] and C++ code.
* Since the {{incode|.ui}} file just describes the "appearance" of the interface, it does not need to be tied to a particular programming language; it may be used both in [[Python|Python]] and C++ code.


=== Interface completely in Python code === <!--T:10-->
For examples on this method see [[Interface_creation_with_UI_files|Interface creation with UI files]].

=== Interface completely in Python code ===


<!--T:11-->
In this method the entire interface is defined by several Python calls.
In this method the entire interface is defined by several Python calls.
* This is an older way of working with interfaces.
* This is an older way of working with interfaces.
Line 33: Line 47:
* It is not simple to separate the interface from the logic that uses that code, meaning that a user would need to run the [[Python|Python]] file in the correct context in order to see how the interface would look.
* It is not simple to separate the interface from the logic that uses that code, meaning that a user would need to run the [[Python|Python]] file in the correct context in order to see how the interface would look.
* This method has the advantage that several interfaces may be contained within a single document, at the expense of making the file very large.
* This method has the advantage that several interfaces may be contained within a single document, at the expense of making the file very large.
* This method is recommended only for small interfaces that don't define more than a few widgets, for example in [[macros|macros]].
* This method is recommended only for small interfaces that don't define more than a few widgets, for example in [[Macros|macros]].


<!--T:12-->
For examples on this method see [[Dialog_creation|Interface creation completely in Python]].
For examples on this method see [[Dialog_creation|Interface creation completely in Python]].


<!--T:14-->
{{Docnav
|[[PySide|PySide]]
|[[Dialog_creation|Dialog creation]]
}}


</translate>
</translate>
{{Powerdocnavi{{#translation:}}}}
{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
{{clear}}
[[Category:Python Code{{#translation:}}]]

Latest revision as of 15:44, 6 November 2021

Introduction

Power users have the possibility of creating interfaces to help them produce complex tools for their custom addons, such as macros or full workbenches.

Interfaces are created using PySide, which is a library for using Qt with Python.

Two general methods to create interfaces, by including the interface in the Python file, or by using .ui files.

Description

There are typically two ways of creating interfaces with PySide.

Interface in a .ui file

In this method the interface is defined in a .ui file (an XML document that defines the structure of the interface), which is then imported into Python code that uses it. This is the recommended approach.

  • It allows the programmer to work with the graphical interface separately from the logic that will use it.
  • It allows anybody to look at the interface alone, that is, the .ui file, without having to run Python code.
  • The .ui file may be designed by anybody without programming knowledge.
  • The .ui interface can be used in a standalone window (modal), or in an embedded window (non-modal); therefore, this method is ideal to create custom task panels.
  • Since the .ui file just describes the "appearance" of the interface, it does not need to be tied to a particular programming language; it may be used both in Python and C++ code.

Interface completely in Python code

In this method the entire interface is defined by several Python calls.

  • This is an older way of working with interfaces.
  • This method produces very verbose code because many details of the interface need to be specified by hand.
  • It is not simple to separate the interface from the logic that uses that code, meaning that a user would need to run the Python file in the correct context in order to see how the interface would look.
  • This method has the advantage that several interfaces may be contained within a single document, at the expense of making the file very large.
  • This method is recommended only for small interfaces that don't define more than a few widgets, for example in macros.

For examples on this method see Interface creation completely in Python.