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
  How does this code create a class? Pedroski55 3 120 8 hours ago
Last Post: Pedroski55
  class definition and problem with a method HerrAyas 2 234 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 275 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 356 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 504 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 645 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 723 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 472 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,826 Jan-24-2024, 08:36 AM
Last Post: annakenna
  question about __repr__ in a class akbarza 4 605 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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