PySide/sv: Difference between revisions

From FreeCAD Documentation
(Created page with "Men en föredragen metod är att skapa ett UI objekt som gör alla inställningar av din widget direkt. Den stora fördelen är att ett sådant UI objekt kan [[Dialog creation...")
(Updating to match new version of source page)
 
(27 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
[http://en.wikipedia.org/wiki/PyQt PyQt] är en python modul som tillåter python applikationer att skapa, komma åt och ändra [http://sv.wikipedia.org/wiki/Qt Qt] applikationer. Du kan använda den för till exempel att skapa dina egna Qt program i python, eller till att komma åt och ändra gränssnittet i en körande qt applikation, som FreeCAD.
|[[Pivy|Pivy]]
|[[Interface_creation|Interface creation]]
}}


{{TOCright}}
Genom att använda PyQt modulen inifrån FreeCAD, så har du därför full kontroll över dess gränssnitt. Du kan till exempel:


==Introduction==
* Lägga till dina egna paneler, widgetar och verktygslådor
* Lägga till eller gömma element i existerande paneler
* Ändra, omdirigera eller lägga till anslutningar mellan alla dessa element


The [[PySide|PySide]] library gives access to the cross-platform graphical user interface (GUI) toolkit Qt from [[Python|Python]]. Qt is a collection of C++ libraries, but with the help of PySide, the same components can be used from [[Python|Python]]. Every graphical interface that can be created in C++, can also be created and modified in Python. An advantage of using Python is that Qt interfaces can be developed and tested live, as we don't need to compile the source files.
PyQt har en extensiv [http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/classes.html API dokumentation], och det finns många övningar på nätet för att lära dig hur det fungerar.


When you install FreeCAD, you should get both Qt and PySide as part of the package. If you are [[Compiling|compiling]] yourself then you must verify that these two libraries are installed in order for FreeCAD to run correctly. Of course, PySide will only work if Qt is present.
Om du vill arbeta på FreeCAD gränssnittet, så är den första saken du ska göra att skapa en referens till FreeCAD's huvudfönster:

In the past, FreeCAD used PyQt, another Qt binding for Python, but in 2013 ([https://github.com/FreeCAD/FreeCAD/commit/1dc122dc9a commit 1dc122dc9a]) the project migrated to PySide because it has a more permissible [[License|License]].

For more information see:
* [https://en.wikipedia.org/wiki/PySide Wikipedia:PySide]
* [https://wiki.qt.io/Differences_Between_PySide_and_PyQt Differences Between PySide and PyQt]

[[File:PySideScreenSnapshot1.jpg]] [[File:PySideScreenSnapshot2.jpg]]
{{Caption|Examples created with PySide. Left: a simple dialog. Right: a more complex dialog with graphs.}}

==PySide in FreeCAD with Qt5==

FreeCAD was developed to be used with Python 2 and Qt4. As these two libraries became obsolete, FreeCAD transitioned to Python 3 and Qt5. In most cases this transition was done without needing to break backwards compatibility.

Normally, the {{incode|PySide}} module provides support for Qt4, while {{incode|PySide2}} provides support for Qt5. However, in FreeCAD there is no need to use {{incode|PySide2}} directly, as a special {{incode|PySide}} module is included to handle Qt5.

This {{incode|PySide}} module is located in the {{incode|Ext/}} directory of an installation of FreeCAD compiled for Qt5.
{{Code|code=
{{Code|code=
/usr/share/freecad/Ext/PySide
import sys
from PySide import QtGui ,QtCore
app = QtGui.qApp
mw = app.activeWindow()
}}
}}

Sedan så kan du till exempel lista igenom gränssnittets alla widgetar:
This module just imports the necessary classes from {{incode|PySide2}}, and places them in the {{incode|PySide}} namespace. This means that in most cases the same code can be used with both Qt4 and Qt5, as long as we use the single {{incode|PySide}} module.
{{Code|code=
{{Code|code=
PySide2.QtCore -> PySide.QtCore
for child in mw.children():
PySide2.QtGui -> PySide.QtGui
print 'widget name = ', child.objectName(), ', widget type = ', child
PySide2.QtSvg -> PySide.QtSvg
PySide2.QtUiTools -> PySide.QtUiTools
}}
}}
Widgetarna i ett Qt gränssnitt är vanligtvis nästlade i "behållar" widgetar, så vårt huvudfönsters barn kan själva innehålla andra barn. Beroende på widgettyp, så finns det många saker du kan göra. Kontrollera API dokumentationen för att se vad som är möjligt.


The only unusual aspect is that the {{incode|PySide2.QtWidgets}} classes are placed in the {{incode|PySide.QtGui}} namespace.
Att lägga till en ny widget, till exempel en dockWidget (som kan placeras i en av FreeCAD's sidopaneler) är lätt:
{{Code|code=
{{Code|code=
PySide2.QtWidgets.QCheckBox -> PySide.QtGui.QCheckBox
myWidget = QtGui.QDockWidget()
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)
}}
}}
{{Top}}
Du kan sedan lägga till saker direkt i din widget:
==Examples of PySide use==
{{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(50,50,200,24)) # sets its size
label.setObjectName("myLabel") # sets its name, so it can be found by name
}}
Men en föredragen metod är att skapa ett UI objekt som gör alla inställningar av din widget direkt. Den stora fördelen är att ett sådant UI objekt kan [[Dialog creation/sv|skapas grafiskt]] med Qt Designer programmet. Ett typiskt objekt som genererats av Qt Designer är som detta:
{{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


* [[PySide_Beginner_Examples|PySide Beginner Examples]], hello world, announcements, enter text, enter number.
self.label = QtGui.QLabel(myWidget) # creates a label
* [[PySide_Intermediate_Examples|PySide Intermediate Examples]], window sizing, hiding widgets, popup menus, mouse position, mouse events.
self.label.setGeometry(QtCore.QRect(50,50,200,24)) # sets its size
* [[PySide_Advanced_Examples|PySide Advanced Examples]], many widgets.
self.label.setObjectName("label") # sets its name, so it can be found by name


The examples of PySide are divided into 3 parts, differentiated by level of exposure to PySide, Python and the FreeCAD internals. The first page has an overview on PySide; the second and third pages are mostly code examples at different levels.
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}}


It is expected that these examples are useful to get started, and afterwards the user can consult other resources online, or the official documentation.
[[Category:Poweruser Documentation]]
{{Top}}
==Documentation==


There are some differences in handling of widgets in Qt4 (PySide) and Qt5 (PySide2). The programmer should be aware of these incompatibilities, and should consult the official documentation if something doesn't seem to work as expected on a given platform. Nevertheless, Qt4 is considered obsolete, so most development should target Qt5 and Python 3.
{{clear}}

<languages/>
The PySide documentation refers to the Python-style classes; however, since Qt is originally a C++ library, the same information should be available in the corresponding C++ reference.
* [https://doc.qt.io/qtforpython/modules.html Qt Modules] available from PySide2 (Qt5).
* [https://doc.qt.io/qt-5/modules-cpp.html All Qt classes by module] in Qt5 for C++.
* [https://deptinfo-ensip.univ-poitiers.fr/ENS/pyside-docs/index.html Qt Modules] available from PySide (Qt4).
{{Top}}

{{Docnav
|[[Pivy|Pivy]]
|[[Interface_creation|Interface creation]]
}}

{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]

Latest revision as of 16:26, 4 April 2024

Introduction

The PySide library gives access to the cross-platform graphical user interface (GUI) toolkit Qt from Python. Qt is a collection of C++ libraries, but with the help of PySide, the same components can be used from Python. Every graphical interface that can be created in C++, can also be created and modified in Python. An advantage of using Python is that Qt interfaces can be developed and tested live, as we don't need to compile the source files.

When you install FreeCAD, you should get both Qt and PySide as part of the package. If you are compiling yourself then you must verify that these two libraries are installed in order for FreeCAD to run correctly. Of course, PySide will only work if Qt is present.

In the past, FreeCAD used PyQt, another Qt binding for Python, but in 2013 (commit 1dc122dc9a) the project migrated to PySide because it has a more permissible License.

For more information see:

Examples created with PySide. Left: a simple dialog. Right: a more complex dialog with graphs.

PySide in FreeCAD with Qt5

FreeCAD was developed to be used with Python 2 and Qt4. As these two libraries became obsolete, FreeCAD transitioned to Python 3 and Qt5. In most cases this transition was done without needing to break backwards compatibility.

Normally, the PySide module provides support for Qt4, while PySide2 provides support for Qt5. However, in FreeCAD there is no need to use PySide2 directly, as a special PySide module is included to handle Qt5.

This PySide module is located in the Ext/ directory of an installation of FreeCAD compiled for Qt5.

/usr/share/freecad/Ext/PySide

This module just imports the necessary classes from PySide2, and places them in the PySide namespace. This means that in most cases the same code can be used with both Qt4 and Qt5, as long as we use the single PySide module.

PySide2.QtCore -> PySide.QtCore
PySide2.QtGui -> PySide.QtGui
PySide2.QtSvg -> PySide.QtSvg
PySide2.QtUiTools -> PySide.QtUiTools

The only unusual aspect is that the PySide2.QtWidgets classes are placed in the PySide.QtGui namespace.

PySide2.QtWidgets.QCheckBox -> PySide.QtGui.QCheckBox

Top

Examples of PySide use

The examples of PySide are divided into 3 parts, differentiated by level of exposure to PySide, Python and the FreeCAD internals. The first page has an overview on PySide; the second and third pages are mostly code examples at different levels.

It is expected that these examples are useful to get started, and afterwards the user can consult other resources online, or the official documentation.

Top

Documentation

There are some differences in handling of widgets in Qt4 (PySide) and Qt5 (PySide2). The programmer should be aware of these incompatibilities, and should consult the official documentation if something doesn't seem to work as expected on a given platform. Nevertheless, Qt4 is considered obsolete, so most development should target Qt5 and Python 3.

The PySide documentation refers to the Python-style classes; however, since Qt is originally a C++ library, the same information should be available in the corresponding C++ reference.

Top