PyQt5.uic.loadUiType() ignores <customwidgets> for main widget class

Kyle Altendorf <[email protected]>
Newsgroups gmane.comp.python.pyqt-pykde
Message-ID <[email protected]>
It looks like presently PyQt5.uic.loadUiType() assumes that the .ui 
file's base widget's class is going to be in PyQt5.QtWidgets.  This is 
not the case for mine which often use a custom type specified in the 
<customwidgets> section.

Rather than:
     return (ui_globals[winfo["uiclass"]], getattr(QtWidgets, 
winfo["baseclass"]))

This seems to work:
     ui_base = ui_globals.get(winfo["baseclass"])

     if ui_base is None:
         ui_base = getattr(QtWidgets, winfo["baseclass"])

     return (ui_globals[winfo["uiclass"]], ui_base)

Example code and output are available at:
     https://gist.github.com/altendky/325d0737a923e6f678617364e0f68f83

Output:
     sys.version: 3.7.4 (default, Aug  2 2019, 12:58:45)
     [GCC 6.3.0 20170516]
     sys.platform: linux
     QT_VERSION_STR: 5.13.0
     PYQT_VERSION_STR: 5.13.0
     SIP_VERSION_STR: 4.19.18
     Traceback (most recent call last):
       File "bad3.py", line 50, in <module>
         main()
       File "bad3.py", line 46, in main
         Ui, UiBase = PyQt5.uic.loadUiType(io.StringIO(ui_text))
       File 
"/home/altendky/st.src/venv/lib/python3.7/site-packages/PyQt5/uic/__init__.py", 
line 204, in loadUiType
         return (ui_globals[winfo["uiclass"]], getattr(QtWidgets, 
winfo["baseclass"]))
     AttributeError: module 'PyQt5.QtWidgets' has no attribute 'MyWidget'

I'll attach my files again here but last time they didn't seem to be 
liked.  If anyone has suggestions on what I should do differently when 
attaching I'm happy to hear them.  I'm using RoundCube webmail.

Cheers,
-kyle

_______________________________________________
PyQt mailing list    [email protected]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
bad3.py (text/x-python, 1.1 KB)
import io
import sys

import PyQt5.QtCore
import PyQt5.QtWidgets
import PyQt5.uic
import sip


system_info = f'''\
sys.version: {sys.version}
sys.platform: {sys.platform}
QT_VERSION_STR: {PyQt5.QtCore.QT_VERSION_STR}
PYQT_VERSION_STR: {PyQt5.QtCore.PYQT_VERSION_STR}
SIP_VERSION_STR: {sip.SIP_VERSION_STR}'''


ui_text = '''\
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="MyWidget" name="Form">
  <layout class="QVBoxLayout" name="layout">
  </layout>
 </widget>
 <customwidgets>
  <customwidget>
   <class>MyWidget</class>
   <extends>QWidget</extends>
   <header>bad3</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>
'''

class MyWidget(PyQt5.QtWidgets.QWidget):
    pass


def main():
    print(system_info)
    app = PyQt5.QtWidgets.QApplication([])

    Ui, UiBase = PyQt5.uic.loadUiType(io.StringIO(ui_text))


if __name__ == '__main__':
    main()


    #exec(code_string.getvalue(), ui_globals)
    #ui_base = ui_globals.get(winfo["baseclass"])
    #if ui_base is None:
        #ui_base = getattr(QtWidgets, winfo["baseclass"])
    #return (ui_globals[winfo["uiclass"]], ui_base)
out (text/x-c, 664 B)
 ~/st.src   boots ●  venv/bin/python bad3.py
sys.version: 3.7.4 (default, Aug  2 2019, 12:58:45) 
[GCC 6.3.0 20170516]
sys.platform: linux
QT_VERSION_STR: 5.13.0
PYQT_VERSION_STR: 5.13.0
SIP_VERSION_STR: 4.19.18
Traceback (most recent call last):
  File "bad3.py", line 50, in <module>
    main()
  File "bad3.py", line 46, in main
    Ui, UiBase = PyQt5.uic.loadUiType(io.StringIO(ui_text))
  File "/home/altendky/st.src/venv/lib/python3.7/site-packages/PyQt5/uic/__init__.py", line 204, in loadUiType
    return (ui_globals[winfo["uiclass"]], getattr(QtWidgets, winfo["baseclass"]))
AttributeError: module 'PyQt5.QtWidgets' has no attribute 'MyWidget'
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.