简体   繁体   中英

PyQ5t: load Qt Designer into Python script (loadUiType): how to check error cause?

I design GUI in Qt Designer, then I load UI-file in my Puthon3 script with the loadUiType method:

class Main(QMainWindow, uic.loadUiType("adc_main_form.ui")[0]):
def __init__(self):
    super(Main, self).__init__()
    self.setupUi(self)

All works fine. Then I make a little revolution in my form design and it includes a lot of renaming. So I take that UI-file of Qt Designer (XML file) and edit it in a text editor. Maybe I make some typo mistakes. Now I get a message during the start of Python script, on the line self.setupUi(self):

File "string", line 671, in setupUi

TypeError: argument 1 has unexpected type 'QRadioButton'

So, some goes wrong in the process of importing the XML file. But the error type tells me not enough to find the error.

I double check all my QRadioButton widgets. No idea.

I open Ui with the Designer - it opens without error messages.

I convert UI into PY (pyiuc5) - no errors.

The .ui file is here .

What can be the way to find the error in such a closed process as setupUI?

In this .ui file the widgets have the same name as the main window's slots. As we subclass the main window, both the widgets and the slots are in the same namespace, so command self.zero_fix = QtWidgets.QRadioButton(self.frame_4) in compiled .ui file overwrite slot zero_fix()

Look at the output generated by pyuic5 , at line 671. It should be inside the setupUi method. That's what the error is referring to. uic.loadUiType is generating a Python file on-the-fly. You then attempt to execute it, and it fails at runtime.

Nitpick: Try removing all of the connections from the .ui file. They don't belong there and make the code much harder to maintain, unless the connections are between widgets within the same file. Simply name MainWindow slots in the following format: on_WIDGET_SIGNAL, where WIDGET is the name of the widget, and SIGNAL is the name of the signal, eg on_setADC1_clicked .

To answer the specific question about how to debug a file loaded by uic :

When you use a method like loadUiType , it creates the same module that is normally generated by pyuic . But instead of writing this module to disk, it calls exec on it, so that the code will be executed dynamically, and the resulting class object can be returned directly.

However, the generated code is not quite identical. When using pyuic , some comment lines are added to the top of the file, and these need to be removed so that the traceback information lines up correctly. To be more specific, the extra lines need to be replaced by a single blank line, so that the top of the module looks like this:

     
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):

With that done, the original traceback now gives line 671 as this one:

    self.zero_fix.clicked.connect(MainWindow.zero_fix)

And the TypeError now makes sense, because self.zero_fix (a radio-button) is now shadowing MainWindow.zero_fix (a slot), due to some bad renaming.

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