4

I am trying to create a new QML type by using Python, but I am having trouble with registering a QML type. However, I am getting an Error:

TypeError: 'PySide2.QtQml.qmlRegisterType' called with wrong argument types:
  PySide2.QtQml.qmlRegisterType(module, str, int, int, str)
Supported signatures:
  PySide2.QtQml.qmlRegisterType(type, str, int, int, str)

So I understand that its expecting a type, however, in this blogpost it does something similar:

qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart')

Which leaves me confused, I don't know what I'm doing wrong?

In my main.py, I have this:

...

if __name__ == '__main__':
    # Declare QApplication
    app=QApplication([])

    qmlRegisterType(CamFeed, 'CFeed', 1, 0, 'CamFeed')

    ...

CamFeed.py looks like this:

from PySide2.QtQuick import QQuickPaintedItem
from PySide2.QtGui import QPainter
from PySide2.QtCore import QObject

class CamFeed(QQuickPaintedItem):
    def __init__(self, parent=None):
        super().__init__(parent)

    # Re-implementation of the virtual function
    def paint(self, painter):
        painter.drawRect(10,10,50,50)

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

6

Surely in the main.py file you are importing the CamFeed.py in the following way:

import CamFeed

if __name__ == '__main__':
    # Declare QApplication
    app = QApplication([])
    qmlRegisterType(CamFeed, 'CFeed', 1, 0, 'CamFeed')

In that case CamFeed is the module (.py file), so there are 2 solutions:

1.

from CamFeed import CamFeed

if __name__ == '__main__':
    # Declare QApplication
    app = QApplication([])
    qmlRegisterType(CamFeed, 'CFeed', 1, 0, 'CamFeed')

2.

import CamFeed

if __name__ == '__main__':
    # Declare QApplication
    app = QApplication([])
    qmlRegisterType(CamFeed.CamFeed, 'CFeed', 1, 0, 'CamFeed')

On the other hand by convention the name of the lowercase:

camfeed.py

from PySide2.QtQuick import QQuickPaintedItem
from PySide2.QtGui import QPainter
from PySide2.QtCore import QObject

class CamFeed(QQuickPaintedItem):
    def __init__(self, parent=None):
        super().__init__(parent)

    # Re-implementation of the virtual function
    def paint(self, painter):
        painter.drawRect(10,10,50,50)

main.py

from camfeed import CamFeed

if __name__ == '__main__':
    # Declare QApplication
    app = QApplication([])
    qmlRegisterType(CamFeed, 'CFeed', 1, 0, 'CamFeed')
eyllanesc
  • 235,170
  • 19
  • 170
  • 241