Unsolved Is this possible?
-
@SPlatten said in Is this possible?:
, which means I wouldn't have to code up and think of every possibly signal up front.
But I think that's precisely what the discussion here is all about! C++ is statically typed, and you cannot think of every possible signal which is what you need at compile-time, and that is the issue! And I/others do not see how using lambdas would obviate that, lambdas are still subject to compile-type type checking, for both their parameters and the methods they might call....
Perhaps we should leave it at that and you see how you get on, because we're not getting very far with a "You say, I say" ... :)
-
@JonB , the reason for Lambda is purely to provide an interface between the C++ and the JavaScript. Using a repeater signal allows me to use the same signal out of the repeater with parameters that will never change, the receiving signal which fires the repeater is the one that will have the differences.
Please don't get me wrong, I'm not arguing with you, I was under the impression that what I'm trying to achieve is actually possible.
-
@SPlatten Let's say you have 3 signals with different parameter lists:
void signal1(int); void signal2(char*, float); void signal3(QString, QVariant, QString);
How will you connect them to ONE slot (as you wrote: "produce a map of available signals then connect all these signals to a single slot in C++") and handle all 3 different parameter lists in that slot? Don't forget: C++ is statically typed language as already pointed out in this thread. You have different parameter number and different parameter types.
-
@SPlatten The way I understand your approach is this:
cpp_signal(int) js_slot(int) cpp_signal(float) js_slot(float) cpp_signal(string, int) -> cpp_signal(common_param) -> cpp_slot(common_param) -> js_slot(string, int) cpp_signal(custom_class) js_slot(custom_class_projection) cpp_signal(whatever) js_slot(whatever_projection)
While you can do the two transitions on the right I don't see how you can accomplish the one on the left. The target of connection takes either the same amount of the same typed parameters as the sender or less. You would have to add an extra layer of forwarding signals between the two columns on the left, but that just means a slot with every possible combination of parameters.
Lambdas have nothing to do with any of that. A lambda, for all intents and purposes of this topic, is just a struct with () operator and follows the same static typing rules as a struct.
That being said I really don't like to say something can't be done, so for the sake of being open minded I would imagine this could be implemented with a new type of connect that in pseudo code works like this:
struct Param { QString name; QVariant value; } using ParamPack = QVector<Param>; connect(sender, &Sender::signal, func);
where
func
is of fixed signature e.g.void (*)(const ParamPack& params)
and connect is a template function that does parameter packing. This packing could in theory be done with some tuple like template shenanigans, but I suspect you'd have to use private Qt headers to get to all the stuff needed.
To be fair, this would only work for an explicit connect statement with explicit signal so that template can take apart its signature. I don't see a way to do that by iterating over a list of signals. There's just no static type information in that case that the template could get to. -
@jsulm, ok if its not possible then I will have to implement my original design which is to produce a slot for each signal type and internal to that slot I will construct an object and then emit the generic signal with the constructed object which will pass on the originating signal details. This signal will be use Lambda.
To illustrate:
C++ signal(int) -> Internal slot (repeater) (int) | C++ signal(float) -> Internal slot (repeater) (float) | C++ signal(string, int) -> Internal slot (repeater) (string, int) |- Emit C++ signal(custom_class) -> Internal slot (repeater) (custom_class) | C++ signal(whatever) -> Internal slot (repeater) (whatever) |
The repeaters will create a JSON object which will include details of the source signal and control associated with the signal. The Emit on the right will be a signal that will be common to all control signals and have only the constructed JSON object built by the repeater.
-
That's doable, but it means you will have to implement a repeater for every possible combination of any types of parameters. Unless you heavily limit possible signal signatures I don't think that's practical.
-
@Chris-Kawa , Yep, thats what I was hoping to avoid, but if its unavoidable, then I'll just have to do it and release patches / updates as I add additional controls to the engine.
-
@SPlatten said in Is this possible?:
then I'll just have to do it and release patches / updates as I add additional controls to the engine.
Originally you wrote:
dynamic map capable of storing the information for any QT control or derived control and it’s signals.
I don't mean to pour cold water/put up obstacles gratuitously for your work. But if you are going to have to release patches for each signal, what happens if I derive my own
QWidget
s and add my own new, dedicated signals? Which I do frequently! -
My project is to create an engine that allows anyone to develop GUI applications for any platform that the engine is available.
Is it NOT intended to allow developers to modify the engine itself. It will allow anyone using the engine to develop GUI applications with XML and JavaScript.
-
@SPlatten
OK, but a limitation is I cannot define any signals in my code? E.g. to go with a new widget I derive. You regard that as "modify the engine itself"? -
That is something I will probably allow, so you can emit signals from JavaScript and connect them to slots in your own JavaScript. All signals that are available in the controls will be assessable via a subscription in XML.
@JonB, think about it as a developer, you want a GUI interface and there are a lot of controls available in Qt all of which you will be able to create and access using XML and JavaScript. If you want additional signals then you can create these in your JavaScript application.
-
@SPlatten Out of curiosity - how is your engine different from just using QML for example? I mean apart from the language syntax (QML vs XML). I'm asking because I rarely use these interpreted technologies (too much resource waste for my taste), but what you're describing sounds similar. Is it just that you prefer XML? There's also a QUiLoader class that can load XML based ui at runtime and you can expose that to a QJSEngine for example. Is what you're doing something more than that?
-
@Chris-Kawa , for starters, any changes you make to QML require a rebuild of the project that references it. The entire point of my offering is that the engine is static and cannot be changed, but the XML and JavaScript can be changed and reloaded into the engine with no recompilation required.
Its also multi-platform, the XML and JavaScript can be ported to any Operating System where the engine is available and with no changes it will work.
-
@SPlatten said:
QML require a rebuild of the project that references it.
It does? I though you can just load it up from any file at runtime. Huh, I guess it just shows how little I use it :) Oh well..
-
@Chris-Kawa , originally thats exactly how I thought QML worked too, however having worked with it now for quite a while, I scratch my head and wonder how or why it evolved in the first place into what it is....because its like everything you can do in QML you can also do in C++, granted its a little simpler but you cannot do it without rebuilding the project.
-
@SPlatten said in Is this possible?:
QML require a rebuild of the project that references it.
It most definitely does not!!
I even expanded my main qml file to tricker a reload via F5 to quickly check visual changes (Sadly not a default feature)A rebuild is only needed, if you move your QML files in a resource file and load them from there, then a change requires recompiling. With local paths no recompile needed.
-
@SPlatten , @Chris-Kawa , also now @J-Hilk
Again, please don't take this as a negative comment! Bear in mind I know nothing about QML! :) But is https://doc.qt.io/qt-5/qml-qtquick-loader.html, https://qmlbook.github.io/ch14-dynamicqml/dynamicqml.html not a dynamic loader without compilation? -
@JonB same principle, you can pass a Loader either a local file url or a qrc url and it will load & evaluate at runtime
-
@Chris-Kawa said in Is this possible?:
That's doable, but it means you will have to implement a repeater for every possible combination of any types of parameters. Unless you heavily limit possible signal signatures I don't think that's practical.
@SPlatten I do understand where you are coming from and templates look like they should be able to do it. However, Qt does not play well with templates. Otherwise you might even be right with your approach. It would be something like templated slots. Would be really nice to have!
Unfortunately, C++ is still lacking some metaprogramming capabilities. In order to avoid too much code duplication for each possible combination of types in your signals' signatures I would in fact suggest using macros. You might want to have a quick look at so called X macros. Basically you would then
#define
a list of signatures for your slots and then let a macro implement all the slots for this list. I would consider this use of macros cleaner than implementing the same over and over again. "Implementing" a new signature is then as easy as adding it to the list. Only disadvantage of this might be when you try to debug it (you cannot step through a multiline macro...).