[PyQt] Multiple inheritance from custom widgets and uic

Denis Rouzaud denis.rouzaud at gmail.com
Thu Aug 30 11:51:44 BST 2018


Hi Alexander,

(forwarding to qgis-dev list, as I think it should be also referenced there)

Here is the reply I got from Phil:

> I can confirm it is a bug, however the fix is non-trivial so it won't get
fixed quickly. The fundamental problem is that the code has the assumption
that all custom widgets will reside in the PyQt5 package hard-coded (but
even then it's not quite as simple as that).

He also wrote it's on his todo list in the next 3-4 weeks on that time, but
I don't think it has been solved.

Best wishes,
Denis

Le jeu. 30 août 2018 à 08:35, Alexander Bruy <alexander.bruy at gmail.com> a
écrit :

> 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
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt

-- 

Denis Rouzaud
denis at opengis.ch  <denis at opengis.ch>
+41 76 370 21 22
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180830/a21c1e98/attachment.html>


More information about the PyQt mailing list