简体   繁体   中英

System Error when running PyQt4's loadUi()

I'm pretty new to Qt, Python and their combinations. I'm currently writing a QGIS plugin in python (I used QtCreator 2.1 (Qt Designer 4.7) to generate a .ui-file and am now trying to use it for a Quantum GIS plugin that's written in Python 2.5 (and running in the Quantum GIS Python 2.5 console)). I am running into trouble when loading the ui-file dynamically when the program runs the loadUi() function. What throws me off is that the error occurs outside my script. Does that mean, I'm passing something wrong into it? Where does the error come in? Any hints on what could be wrong?

code_dir = os.path.dirname(os.path.abspath(__file__)) 
self.ui = loadUi(os.path.join(code_dir, "Ui_myfile.ui"), self)

This is the Error Code I am getting (minus the first paragraph):

File "C:/Dokumente und Einstellungen/name.name/.qgis/python/plugins\\myfile\\myfile_gui.py", line 42, in __ init __ self.ui = loadUi(os.path.join(code_dir, "Ui_myfile.ui"), self)

File "C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\PyQt4\\uic__init__.py", line 112, in loadUi return DynamicUILoader().loadUi(uifile, baseinstance)

File "C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\PyQt4\\uic\\Loader\\loader.py", line 21, in loadUi return self.parse(filename)

File "C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\PyQt4\\uic\\uiparser.py", line 768, in parse actor(elem)

File "C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\PyQt4\\uic\\uiparser.py", line 616, in createUserInterface self.traverseWidgetTree(elem)

File "C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\PyQt4\\uic\\uiparser.py", line 594, in traverseWidgetTree handler(self, child)

File "C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\PyQt4\\uic\\uiparser.py", line 233, in createWidget topwidget.setCentralWidget(widget) SystemError: error return without exception set

I'm not sure of what could be causing this precise problem, but using .ui files directly has never worked well for me - instead I compile them to python code using pyuic4 (should be in your path if your PyQt4 site-packages are correctly configured. The syntax is along the lines of :-

pyuic4 -o <python output> -x <uic input>

-: resource files can similarly be converted to (not so human-readable) python using :-

pyrrc4 -o <python output> <qrc input>

-: You can then import that python file as a module, not to mention that reading its code can give you clues on how to fiddle with the layout at runtime.

The other upside of this is that you are not having to parse xml at runtime - importing a python module is far quicker and you are not likely to change the ui anywhere near as often as you run the script. If you do find yourself in this situation you can just create a batch to run these before your script/application.

This is the method I use in conjunction with pyInstaller to deploy scripts that will 'just run' on XP, Vista and Windows 7 without modification, and I generate the python modules just before compiling with pyInstaller, so it can be a real time saver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM