Skip to content

Using enum elements from C++ Qt Object in python #71

Description

@kwisp

Short

In Python not found enum elements from C++ class derived from QObject added to PythonQt via addDecorators() and registerClass(QMetaObject*).

Versions

Qt 5.12.8
Python 3.8.10
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
PythonQt master fb16d6e on 3 Nov 2021

Example Code

Add to std example PyDecoratorsExample
ExternalWidget.h

#ifndef EXTERNAL_W_HH_
#define EXTERNAL_W_HH_

#include <QtWidgets/QWidget>

class ExternalWidget : public QWidget
{
  Q_OBJECT

public:
  enum EType
  {
    Aone,
    Atwo,
    Athree,
    Afour
  };

  Q_ENUM(EType)

  ExternalWidget(QWidget* parent = nullptr);
  ~ExternalWidget();

public slots:
  void widgetSlot(){};
};

#endif  // EXTERNAL_W_HH_

ExternalWidget.cpp

#include "ExternalWidget.h"

ExternalWidget::ExternalWidget(QWidget* parent) : QWidget(parent)
{
}

ExternalWidget::~ExternalWidget()
{
}

add to main.cpp

PythonQt::self()->registerClass(&ExternalWidget::staticMetaObject, "QtGui");

add to example.py

w = QtGui.ExternalWidget()
w.move(100,100)
w.show()

add ExternalWidget.h and ExternalWidget.cpp to HEADERS a SOURCES in PyDecoratorsExample.pro
In python console can use
dir(QtGui.ExternalWidget)

There are no enum elements Aone, Atwo e.t.c. and enum EType

Whats wrong???
I try to use Q_ENUMS() and result is the same. :(

Activity

  1. ChenxiLiu-code commented on Apr 29, 2025

    @ChenxiLiu-code

    I think you may need to provide a self-defined wrapper class for your own class.
    And define the enums in the wrapper class.
    My test code is below:

    wrapper code:

    class TestCppObj {
    public:
    ///... other functions
    };
    
    class TestCppObjWrapper : public QObject {
        Q_OBJECT
    public:
        enum TestEnum { A, B, C };  /// define enums here!
        Q_ENUMS(TestEnum)
    
    public slots:
        ///... other slots for this type

    register code:

    PythonQt::self()->registerCPPClass("TestCppObj", "", "TestUtils", PythonQtCreateObject<TestCppObjWrapper>, PythonQtSetInstanceWrapperOnShell<TestCppObjShell>);

    python code:

    print(dir(PythonQt.TestUtils.TestCppObj))

    output:

    ['A', 'B', 'C', 'TestEnum',  #the defined enums are here!
    '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'className', 'delete', 'help', 'inherits', 'passToCPP', 'print']
  2. usiems commented on Apr 30, 2025

    @usiems
    Contributor

    Defining a custom wrapper class for a QObject-derived class should not be necessary if all required methods already are slots, since then PythonQt can determine all necessary information from the data in QMetaType. The same normally goes for enum types that are registered with Q_ENUM.

    This issue is quite old, so I don't know if this is still relevant. Usually the enum type and the values should be available, since they are derived from the QMetaType, so I have no explanation for this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions