Converting c++ class to python class - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Converting c++ class to python class (/thread-4007.html) Pages:
1
2
|
Converting c++ class to python class - panoss - Jul-16-2017 I 'm trying to convert a c++ class to python class. My class 's c++ code (in epnccombomodel.cpp file): #include "epnccombomodel.h" EPNCComboModel::EPNCComboModel(QObject *parent) : QAbstractProxyModel(parent) { }My class 's python code (in epnccombo.py file): from PyQt4.QtCore import QObject from PyQt4.QtGui import QAbstractProxyModel class EPNCComboModel(QObject) : def __init__(self, parent): QAbstractProxyModel(EPNCComboModel, self).__init__(parent)But I get an error (TypeError: __init__() missing 1 required positional argument: 'parent') on initialization: EPNCproxyModel = EPNCComboModel()First of all, this line: EPNCComboModel::EPNCComboModel(QObject *parent) : QAbstractProxyModel(parent) What exactly is it saying? Create a Class deriving from QObject? (child of QObject) And variable 'parent' equals to this QObject? RE: Converting c++ class to python class - Larz60+ - Jul-16-2017 In your c++ code, QAbstractProxyModel(parent) is a named method, so should also be in python from PyQt4.QtCore import QObject from PyQt4.QtGui import QAbstractProxyModel class EPNCComboModel(QObject) : def __init__(self, parent): def __init__(parent): self.parent = parent def QAbstractProxyModel(self, EPNCComboModel): # when used here, self.parent will reference the instance in __init__ RE: Converting c++ class to python class - panoss - Jul-17-2017 I used your code, slightly modified (line 5, 'parent=None' and line 10, 'pass'): from PyQt4.QtCore import QObject from PyQt4.QtGui import QAbstractProxyModel class EPNCComboModel(QObject) : def __init__(self, parent=None): def __init__(parent): self.parent = parent def QAbstractProxyModel(self, EPNCComboModel): pass # when used here, self.parent will reference the instance in __init__Now I get the error: "RuntimeError: super-class __init__() of type EPNCComboModel was never called" I suppose I must use something like: super(EPNCComboModel, self).__init__(parent) ? RE: Converting c++ class to python class - Larz60+ - Jul-17-2017 from PyQt4.QtCore import QObject from PyQt4.QtGui import QAbstractProxyModel class EPNCComboModel(QObject) : def __init__(self, parent=None): self.QAbstractProxyModel(parent) def QAbstractProxyModel(self, parent): passThis should be right also, use 4 spaces for indent (PEP8) RE: Converting c++ class to python class - panoss - Jul-17-2017 No, I get the same error. RE: Converting c++ class to python class - Larz60+ - Jul-17-2017 please post full error traceback RE: Converting c++ class to python class - panoss - Jul-17-2017 This is it (at line 11 is the error): C:\Python34\python.exe C:/Users/Pangs/PycharmProjects/repairdevicesv7_qt/main.py Database: connection ok 0.RepairDevices c= 0 Traceback (most recent call last): File "C:/Users/Pangs/PycharmProjects/repairdevicesv7_qt/main.py", line 2241, in <module> window = MyApp() File "C:/Users/Pangs/PycharmProjects/repairdevicesv7_qt/main.py", line 78, in __init__ self.setupSettings() File "C:/Users/Pangs/PycharmProjects/repairdevicesv7_qt/main.py", line 357, in setupSettings EPNCproxyModel.setSourceModel(parts_tbl_model) RuntimeError: super-class __init__() of type EPNCComboModel was never called QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread QObject::startTimer: QTimer can only be used with threads started with QThread Process finished with exit code 1 RE: Converting c++ class to python class - Larz60+ - Jul-17-2017 Quote:No, I get the same error.That's not the same error! Is this functionality unavailable in PyQt5 5.9 ? RE: Converting c++ class to python class - panoss - Jul-17-2017 Larz60, sorry but I don't understand what you 're saying. Which functionality and which PyQt5? I 'm using only PyQt4. QAbstractProxyModel is a class (a Qt model class). RE: Converting c++ class to python class - panoss - Jul-18-2017 Finally I just made the class inherit QAbstractProxyModel, so an excact 'translation' of C++ class to python class was not achieved, but it 's functioning correctly. So I ended up to: class EPNCComboModel(QAbstractProxyModel): def __init__(self, parent=None): super(EPNCComboModel, self).__init__(parent)But how do I 'translate' this to python? QModelIndex EPNCComboModel::mapFromSource(const QModelIndex & sourceIndex) const |