pythonqt icon indicating copy to clipboard operation
pythonqt copied to clipboard

Using enum elements from C++ Qt Object in python

Open kwisp opened this issue 3 years ago • 0 comments

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. :(

kwisp avatar May 31 '22 15:05 kwisp