Python Forum

Full Version: Converting c++ class to python class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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?
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__
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) ?
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):
        pass
This should be right also, use 4 spaces for indent (PEP8)
No, I get the same error.
please post full error traceback
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
Quote:No, I get the same error.
That's not the same error!
Is this functionality unavailable in PyQt5 5.9 ?
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).
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
Pages: 1 2