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.7k 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.
  • SGaistS SGaist

    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 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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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 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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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 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 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
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 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 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
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 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 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

                          • Login

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