Reputation: 27
I am trying to convert a Qt4 custom widget written in C++ to a Python 2.7 custom widget. However, I have not been able to figure out how QLabel(parent)
would be written in Python. This is the original C++ code from the ".ccp" file:
DocumentWidget::DocumentWidget(QWidget *parent)
: QLabel(parent)
{
currentPage = -1;
setAlignment(Qt::AlignCenter);
}
The QLabel(parent)
seems to be some sort of initializer list. I've tried using multiple inheritance in Python in parallel, but this leads to the following error: Cannot create a consistent method resolution order (MRO) for bases QLabel, QWidget
.
I'm trying to port the code instead of creating a wrapper for the C++ widget, because I don't know C++ and think I will have to customize the widget further in the future.
I'm not trained as a programmer and this is the first day I ran into C++, so feel free to correct me even if I'm doing something silly. I will not feel embarrassed.
Upvotes: 0
Views: 618
Reputation: 27
Multiple inheritance worked, but the base classes had to be called in the correct order (i.e., DocumentWidget(QLabel, QWidget)
instead of DocumentWidget(QLabel, QWidget)
).
In full:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class DocumentWidget(QLabel, QWidget):
def __init__(self, parent=None):
super(DocumentWidget, self).__init__()
self.currentPage = -1
self.setAlignment(Qt.AlignCenter)
Upvotes: 0
Reputation: 120798
The code defines a constructor for the DocumentWidget
class, which inherits QLabel
and requires a QWidget
as parent.
The equivalent PyQt code would be:
from PyQt4 import QtCore, QtGui
class DocumentWidget(QtGui.QLabel):
def __init__(self, parent):
super(DocumentWidget, self).__init__(parent)
# or QtGui.QLabel.__init__(self, parent)
self.currentPage = -1
self.setAlignment(QtCore.Qt.AlignCenter)
Upvotes: 3