Dynamically Loading UI With Unspecified Slots
Hi. I want to dynamically load a .ui file. I know to use QUILoader.
Supposing the widget the .ui file describes new signals and slots. If this was a compile-time problem, I could generate the ui_foo.h and implement the slot in code.
Since this is all dynamic, when I load the .ui file, the widget will have slots with no definitions. I have constructed a simple example:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="my_widget">
<widget class="QPushButton" name="pushButton">
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>Form</receiver>
<slot>my_slot()</slot>
</connection>
</connections>
<slots>
<slot>my_slot()</slot>
</slots>
</ui>
Now suppose I do something like this:
Code:
QFile file("my_widget.ui");
file.
open(QFile::ReadOnly);
= QWidget * my_widget
= loader.
load(&file,
this);
What happens when I click the button? my_slot() isn't defined.
I want to intercept calls to my_slot but I have no idea how. Something clever with QMetaObject maybe?
Re: Dynamically Loading UI With Unspecified Slots
Instead of unpacking the ui to a plain QWidget (my_widget in your case), unpack it to a subclass of QWidget that has a slot named my_slot.
Re: Dynamically Loading UI With Unspecified Slots
The unpacking code doesn't have the subclass definition, that's the whole point, it's all going into scripting. The constraint is that the top-level class ONLY in the .ui file may be an arbitrary new class, everything which it includes must be available to the unpacker.
Essentially, the .ui is guiding the arrangement of known classes and I need to hook into the new signals and slots.