Unsolved Subclass QUiLoader
-
I am subclassing QUiLoader so that I can control the widgets created in createWidget. My code is something like this:
from importlib import import_module class MyLoader(QUiLoader): def __init__(self): super().__init__() self.widget_mod = import_module("PySide2.QtWidgets") def createWidget(self, class_name, parent=None, name=''): orig_widget_inst = QUiLoader.createWidget(self, class_name, parent=None, name='') my_widget_inst = getattr(self.widget_mod, class_name)(parent) return my_widget_inst
my_widget_inst appears to be equivalent to orig_widget_inst, but mine causes a segmentation fault. What else do I need to do to make my_widget_inst usable?
-
Hi,
@3beezer said in Subclass QUiLoader:
def createWidget(self, class_name, parent=None, name=''):
orig_widget_inst = QUiLoader.createWidget(self, class_name, parent=None, name='')
widget_widget_inst = getattr(self.widget_mod, class_name)(parent)
return my_widget_instYou are talking about my_widget_inst in your post but you don't have anything named like that created in your code. Is it just a typo ?
-
@sgaist Typo. I fixed it. Sorry.
-
Can you add a minimal sample code that uses this class and triggers the issue ?
-
import sys from importlib import import_module from PySide2.QtCore import QFile from PySide2.QtUiTools import QUiLoader from PySide2.QtWidgets import QApplication class MyLoader(QUiLoader): def __init__(self): super().__init__() self.widget_mod = import_module("PySide2.QtWidgets") def createWidget(self, class_name, parent=None, name=''): orig_widget_inst = QUiLoader.createWidget(self, class_name, parent=None, name='') print("orig_widget_inst =", orig_widget_inst) widget_class = getattr(self.widget_mod, class_name) my_widget_inst = widget_class(parent) print("my_widget_inst =", my_widget_inst) return orig_widget_inst #return my_widget_inst if __name__ == "__main__": app = QApplication(sys.argv) ui_file = QFile(sys.argv[1]) ui_file.open(QFile.ReadOnly) loader = MyLoader() loader.load(ui_file) ui_file.close()
and test.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>test_panel</class> <widget class="QWidget" name="test_panel"/> <resources/> <connections/> </ui>
Run the program using
python test.py test.ui
Switch the comment on the two return statements to see the crash.
-
I'm new here. Should I report this problem as a bug? If so, where?
-
What version of PySide2 are you currently using ?
On what OS ?PySide2 5.11.1 and 5.13.0 on macOS are working with your test example.
-
@sgaist Interesting! I am using version 5.12.4 on Kubuntu 19.04 with Python 3.7.3. Oh, I see that I can upgrade PySide2 to 5.13.0 using pip. I will do so and report back.
-
Nope. Still crashes. You did switch the # to the other return, right?
-
No I didn't because I overlooked it and I must say I expect to run crashing examples with the "working" version in comment rather than the contrary.
Before digging further, can you just explain what you want to do exactly in
createWidget
? -
I would like to wrap the widget and return an instance of the wrapped widget. Since I can't return an instance of the exact same widget without getting a crash, it doesn't seem to be possible to override createWidget, despite claims to the contrary.
-
What wrapping do you have in mind ?
-
I have several ideas. One I would have liked to implement earlier today is a subclass with
__del__
so that I can confirm that I haven't created a cyclic reference. I do this in portions of the GUI I design manually, but I can't do it in portions I design using Qt Designer. -
I wonder if it's a python specific issue.
Can you test if the same issue happens in C++ ?
-
I think it is, but I don't know C++ well enough to write a test program.
-
The problem is officially a bug: