Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Converting c++ class to python class
Forum Updated to NodeBB v4.3 + New Features

Converting c++ class to python class

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Panoss
    wrote on 16 Jul 2017, 10:48 last edited by Panoss
    #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.
    I initialize it this way: 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?

    J 1 Reply Last reply 17 Jul 2017, 04:34
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 16 Jul 2017, 21:27 last edited by
      #2

      Hi,

      Do you mean you just want to translate or do you want to create a wrapper on top of your C++ code ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply 17 Jul 2017, 13:31
      0
      • P Panoss
        16 Jul 2017, 10:48

        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.
        I initialize it this way: 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?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 17 Jul 2017, 04:34 last edited by
        #3

        @Panoss said in Converting c++ class to python class:

        def init(self, parent)

        Your constructor requires a non optional parameter, so you have to give one:

        EPNCproxyModel = EPNCComboModel(some_parent_object)
        

        Parent can be nullptr in C++ if you do not want to have a parent by default (but you can still provide one if needed):

        EPNCComboModel(QObject *parent=nullptr)
        

        In this case you can omit parent.
        In Python:

        def __init__(self, parent=None):
        

        Read this about child/parent: http://doc.qt.io/qt-5.9/objecttrees.html

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • S SGaist
          16 Jul 2017, 21:27

          Hi,

          Do you mean you just want to translate or do you want to create a wrapper on top of your C++ code ?

          P Offline
          P Offline
          Panoss
          wrote on 17 Jul 2017, 13:31 last edited by Panoss
          #4

          @SGaist said in Converting c++ class to python class:

          Hi,

          Do you mean you just want to translate or do you want to create a wrapper on top of your C++ code ?

          Not a wrapper of C++.
          I want to create a python class that will behave excactly like my C++ function.
          So I thought the best way would be to 'translate' C++ to Python.

          Up to here, I think we are ok:

          class EPNCComboModel(QObject):
              def __init__(self, parent=None):
          

          But QAbstractProxyModel has to 'get into the game' somehow.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 17 Jul 2017, 21:32 last edited by
            #5

            No you're not. Why doesn't EPNCComboMode inherit QAbstractProxyModel ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • P Offline
              P Offline
              Panoss
              wrote on 18 Jul 2017, 14:15 last edited by Panoss
              #6

              This is what I finally did, I made my class inherit QAbstractProxyModel.

              Now, another part of the class' code.
              How do I 'translate' this:

              QModelIndex EPNCComboModel::mapFromSource(const QModelIndex & sourceIndex) const
              

              to python?

              The hard part for me is this: QModelIndex & sourceIndex
              Maybe in python becomes??:

              sourceIndex
              
              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 18 Jul 2017, 22:13 last edited by SGaist
                #7

                It's then just

                mapped = model.mapFromSource(sourceIndex)
                

                [edit: Fixed variable name SGaist]

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • P Offline
                  P Offline
                  Panoss
                  wrote on 19 Jul 2017, 11:13 last edited by
                  #8

                  Hi SGaist.
                  what is map.p?

                  I think the correct code is:

                  def mapToSource(self, proxyIndex):
                  

                  It seems to work. ('self' is like 'this' in C++. It's used (in a class' function) in order to refer to the class)

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 19 Jul 2017, 11:50 last edited by
                    #9

                    That was a typo.

                    Indeed the python function signature is def mapToSource(self, proxyIndex):.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Panoss
                      wrote on 20 Jul 2017, 14:10 last edited by Panoss
                      #10

                      How about this?
                      This is from optiongroup.h file:

                      class OptionGroup : public QWidget
                      {
                      Q_OBJECT
                      Q_PROPERTY(int currentSelection READ currentSelection WRITE setCurrentSelection USER true)
                      

                      This is from optiongroup.cpp file:

                      OptionGroup::OptionGroup(QWidget *parent) :QWidget(parent), currentSelection_(-1)
                      {
                      }
                      

                      How can I convert it to python?

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        Panoss
                        wrote on 21 Jul 2017, 12:08 last edited by
                        #11

                        How does this look? Am I on the right way?

                        class OptionGroup(QtGui.QWidget) :
                            def __init__(self, parent):
                                super(OptionGroup, self).__init__(parent)
                                self.currentSelection_ = -1
                                self.currentSelection = pyqtProperty(int, self.getCurrentSelection, self.setCurrentSelection)
                        

                        So this:

                        Q_PROPERTY(int currentSelection READ currentSelection WRITE setCurrentSelection USER true)
                        

                        equals to this?

                        self.currentSelection = pyqtProperty(int, self.getCurrentSelection, self.setCurrentSelection)
                        
                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 21 Jul 2017, 22:11 last edited by
                          #12

                          You have an example in the PyQt5 documentation.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            Panoss
                            wrote on 22 Jul 2017, 12:19 last edited by Panoss
                            #13

                            The instances of this class are mapped to a QWidgetDataMapper.
                            The problem is that it seems that they don't 'bind' to the QWidgetDataMapper.

                            In the C++ class, I think, that this job does this line:

                            OptionGroup::OptionGroup(QWidget *parent) :QWidget(parent), currentSelection_(-1)
                            

                            I think it means: 'set property for mapping the property named "currentSelection_" '.

                            So I think (again :) ) that my problem is that I can't do the same in Python.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 22 Jul 2017, 20:07 last edited by
                              #14

                              You have to mark the property as USER like explained in the QDataWidgetMapper documentation.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1
                              • P Offline
                                P Offline
                                Panoss
                                wrote on 23 Jul 2017, 07:22 last edited by Panoss
                                #15

                                I changed this line from:

                                currentSelection = pyqtProperty(int, currentSelection, setCurrentSelection)
                                

                                ...to:

                                currentSelection = pyqtProperty(int, currentSelection, setCurrentSelection, user = True)
                                

                                and it works! I can't believe I was struggling for so many days for 2 f***ng words!

                                SGaist you can't imagine how much you helped me. THANK YOU!

                                1 Reply Last reply
                                0

                                1/15

                                16 Jul 2017, 10:48

                                • Login

                                • Login or register to search.
                                1 out of 15
                                • First post
                                  1/15
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved