[PyQt] Multiple inheritance from custom widgets and uic
Alexander Bruy
alexander.bruy at gmail.com
Thu Aug 30 06:34:28 BST 2018
Hi all,
usually I use following code to create a form from the Qt Designer's .ui
file dynamically
from PyQt5 import uic
FORM_CLASS, BASE_CLASS = uic.loadUiType('/path/to/ui/file.ui')
class MyDialog(BASE_CLASS, FORM_CLASS):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
self.setupUi(self)
and everything works fine.
But seems this approach seems does not work with multiple inheritance.
There is a custom dialog class (QDialog subclass with custom UI and some
logic) available in the API library. I want to subclass this custom dialog
and also apply my own UI to it. I tried following code
from PyQt5 import uic
from api.ui import CustomDialogBase
FORM_CLASS, BASE_CLASS = uic.loadUiType('/path/to/ui/file.ui')
class MyDialog(BASE_CLASS, CustomDialogBase, FORM_CLASS):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
self.setupUi(self)
But this raises type error
TypeError: Cannot create a consistent method resolution order (MRO)
for bases QDialog, QgsOptionsDialogBase, Ui_OptionsDialog
As I understand, this is because all classes have the same base class QDialog.
Also I tried to specify only one parent class — custom dialog, as it is
already subclassed from QDialog
from PyQt5 import uic
from api.ui import CustomDialogBase
FORM_CLASS, BASE_CLASS = uic.loadUiType('/path/to/ui/file.ui')
class MyDialog(CustomDialogBase, FORM_CLASS):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
self.setupUi(self)
But this also does not work, with the error
AttributeError: 'MyDialog' object has no attribute 'groupBox'
As I understand, in this case issue is that FORM_CLASS is a QDialog subclass,
not CustomDialogBase's subclass. After some searching I have found this
thread
https://riverbankcomputing.com/pipermail/pyqt/2017-October/039647.html.
Am I right that currently the only way to solve my issue is to construct
form from the compiled .ui file?
Thanks
--
Alexander Bruy
More information about the PyQt
mailing list