From 4fc0f30d5a22ea0f6ca13ce06e9770ce7efa9e00 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 16 Jun 2025 10:29:30 +0200 Subject: Document property/enum usage for Qt Widgets Designer custom widgets Task-number: PYSIDE-2840 Change-Id: I28ca7d163bd560ebb38fb693750e4c3e3b7de621 Reviewed-by: Cristian Maureira-Fredes --- .../doc/tutorials/basictutorial/uifiles.rst | 36 +++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'sources/pyside6/doc/tutorials/basictutorial') diff --git a/sources/pyside6/doc/tutorials/basictutorial/uifiles.rst b/sources/pyside6/doc/tutorials/basictutorial/uifiles.rst index 32d32300c..6d17f0397 100644 --- a/sources/pyside6/doc/tutorials/basictutorial/uifiles.rst +++ b/sources/pyside6/doc/tutorials/basictutorial/uifiles.rst @@ -283,7 +283,7 @@ the custom widget should be visible in the widget box. For advanced usage, it is also possible to pass the function an implementation of the class QDesignerCustomWidgetInterface instead of the type to :meth:`addCustomWidget()`. -This is shown in taskmenuextension example, where a custom context menu +This is shown in the :ref:`example_designer_taskmenuextension`, where a custom context menu is registered for the custom widget. The example is a port of the corresponding C++ `Task Menu Extension Example `_ . @@ -291,6 +291,40 @@ corresponding C++ .. _QDesignerCustomWidgetCollectionInterface: https://doc.qt.io/qt-6/qdesignercustomwidgetcollectioninterface.html .. _QDesignerCustomWidgetInterface: https://doc.qt.io/qt-6/qdesignercustomwidgetinterface.html +Writing Custom Widgets +++++++++++++++++++++++ + +For properties to become visible in *Qt Widgets Designer*, they need to be +declared using :class:`PySide6.QtCore.Property`. + +Enums and flag types need to appear within the class and be decorated using +:deco:`PySide6.QtCore.QEnum` or :deco:`PySide6.QtCore.QFlag`, respectively. +This requires extracting a base class for them since otherwise, the enum type +is not known when specifying :class:`PySide6.QtCore.Property`: + +.. code-block:: python + + class CustomWidgetBase(QObject): + @QEnum + class TestEnum(Enum): + EnumValue0 = 0 + EnumValue1 = 1 + + + class CustomWidget(CustomWidgetBase): + def __init__(self, parent=None): + super().__init__(parent) + self._testEnum = CustomWidget.TestEnum.EnumValue1 + + def testEnum(self): + return self._testEnum + + def setTestEnum(self, new_val): + self._testEnum = new_val + + testEnum = Property(CustomWidgetBase.TestEnum, testEnum, setTestEnum) + + Troubleshooting the Qt Widgets Designer Plugin ++++++++++++++++++++++++++++++++++++++++++++++ -- cgit v1.2.3