Python Forum
Converting c++ class to python class
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting c++ class to python class
#1
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?
Reply
#2
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__
Reply
#3
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) ?
Reply
#4
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)
Reply
#5
No, I get the same error.
Reply
#6
please post full error traceback
Reply
#7
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
Reply
#8
Quote:No, I get the same error.
That's not the same error!
Is this functionality unavailable in PyQt5 5.9 ?
Reply
#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).
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class Blockage not projecting Azdaghost 1 343 May-15-2025, 04:32 PM
Last Post: deanhystad
  Return a string or byte object from Enum class? Calab 5 599 May-14-2025, 05:21 PM
Last Post: snippsat
  Python inner classes inheritance from parent class Abedin 8 1,233 Apr-23-2025, 05:56 AM
Last Post: Gribouillis
  Accessing method attributes of python class Abedin 6 1,347 Apr-14-2025, 07:02 AM
Last Post: buran
  Python class members based on a type value voidtrance 7 1,483 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  Create a new subclass in a Python extension based on an existing class voidtrance 6 1,578 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  printing/out put issue with class arabuamir 3 1,133 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 4,209 Jul-17-2024, 11:12 AM
Last Post: felixandrea
  [split] Class and methods ebn852_pan 15 3,638 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,413 May-13-2024, 05:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020