PDA

View Full Version : Dynamically Loading UI With Unspecified Slots



spraff
31st March 2010, 21:51
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:


<?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:

QUiLoader loader;
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?

axeljaeger
3rd April 2010, 15:51
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.

spraff
11th April 2010, 10:46
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.