Solved Converting c++ class to python class
-
@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.
-
No you're not. Why doesn't
EPNCComboMode
inheritQAbstractProxyModel
? -
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
-
It's then just
mapped = model.mapFromSource(sourceIndex)
[edit: Fixed variable name SGaist]
-
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)
-
That was a typo.
Indeed the python function signature is
def mapToSource(self, proxyIndex):
. -
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?
-
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)
-
You have an example in the PyQt5 documentation.
-
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.
-
You have to mark the property as
USER
like explained in the QDataWidgetMapper documentation. -
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!