Sure, but then you can't access the widgets with type safety. You need calls to findChild() and casts to the correct types (which you must know). |
I don't think so. First of all, you can simply use the widget as a
QWidget*, which allows for all the "standard" widget operations to be called. Secondly, if you really need the concrete sub-class, you can use the
template-version of
QObject::findChild() to get a pointer of the specified type - or
null, if
no such widget exist.
In other words:
dynamic_cast<>() is
not usually needed.
But, most important: Qt is heavily based on the
"Signals & Slots" concept. So, the standard way of "connecting" a loaded widget (from the
.ui file) to your back-end code would be the
QObject::connect() method. This works with any generic
QObject* pointers; the concrete widget type is
not required. Still, it should be perfectly type-safe, as
connect() would simply fail, if the parameter types of the specified signal and slot don't match.
Yeah, you still need to know
beforehand which Signal and/or Slot of the loaded widget you want to connect to. But, I don't see how this could be avoided. I think the Signals/Slots are part of the "interface" that the loaded widget is required to implement. The only real alternative would be embedding all the callback code into the
.ui file, which is exactly what QML and FXML allow (they choose JavaScript as scripting language here).
Yuck! I'd rather call findChild() and do dynamic_cast everywhere than use JS. |
I'm not a fan of JavaScript at all. But what else should be used? If you want to embed
executable code into a
dynamically-loaded UI definition file, then that code
must be written in a scripting language that the GUI Framework can interpret
at runtime. For JavaScript we have various engines (V8, SpiderMonkey, GraalVM, etc.) that can easily be embedded into a GUI Framework, as a library, to choose from. If you wanted to do the same thing with C/C++ code, you'd have to ship a complete C/C++ compiler alongside with your application. Another advantaged of JavaScript is that it can easily be "sandboxed", which does
not apply to C/C++. This means that allowing C/C++ code in the
dynamically-loaded UI definition file would be a new attack vector...