Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Connect Prototype help



  • I've seen that the Qt connect method is able to connect 'any' signal prototype to a slot with the same prototype.

    How is this declared? I need to do something similar in that I have many signals I want to connect to a lambda slot, the slot isn't a problem because the connect method allows any signal prototype to be connected to the lambda slot, what I want to achieve is the following:

    1. All signals will be added to a map that is keyed with a unique identifier, the object stored in the map will contain a pointer to the signal originator and the address of the signal.
    2. Process the signals map, mapping all signals to a single JavaScript slot using the Lambda syntax.

    The question is how to I create a reference that is capable of storing any signal in the same way the connect prototype will accept any signal? I've looked at the prototype and it isn't clear to me which one is actually being used.


  • Qt Champions 2019

    @SPlatten What do you mean by "prototype"? Do you mean different overloads of connect()?
    If you use old connect syntax (with SIGNAL/SLOT) you can store the signal and slot as char*.



  • @jsulm , yes

    What I want to do is have one common connect method where I can iterate through the signals connecting them all to the same JavaScript slot and because of the power and flexibility of how the slot is created I can create a JSON parameter that includes the signal specifics.


  • Qt Champions 2019

    @SPlatten I don't really understand what you want to achieve, but as I said using old connect syntax you can connect any signal to any slot stored as char* as long as they are compatible:

    char* signal = "clicked(bool)";
    char* slot = "doSomething(bool)";
    connect(ui->someButton, SIGNAL(signal), this, SLOT(slot));
    


  • @jsulm , thank you. Is there an implementation of the connect that will allow this signal syntax with a lambda slot?


  • Qt Champions 2019

    @SPlatten https://doc.qt.io/qt-5/qobject.html
    Only with the new connect syntax.



  • @jsulm , thats what I thought, so back to the requirement, can I roll my own and create a connect which does exactly what I required?

    Is there a way to translate the SIGNAL(signal) into the new syntax?



  • So the penny has dropped now, I've been working with C since the mid 80's and C++ since the early 90's. The language has changed quite a lot over the years and there are elements of the language that I have never had to use, however with the introduction of some of the newer enhancements its time to learn and adopt some of these features.

    Specifically templates, I've used them when developing Qt applications without really paying to much attention. Now I find its an absolute requirement if I'm to implement the kind of features I want in my application.

    I want to be able to support any signal that a Qt widget has and connect ever signal to the same Lambda slot, this is going to require some cool code and I know it's possible as thats exactly what the standard connect does.

    I'm currently working through online tutorials, any help or input is greatly appreciated.


  • Lifetime Qt Champion

    Hi,

    Depending on your goal with that, did you consider using some meta object programming ?



  • @SGaist , please tell me more...


  • Lifetime Qt Champion

    It's all in the link ;-)

    The idea is that you can use introspection of your QObject based classes. You can get all the signals of your class, or go upper the chain and get the signals of the base classe (for as many class as your hierarchy has). Then you can use the various QMetaXXX classes to do what you want.



  • Ok, to summarise, what I want to do is reduce all signal and slot connections to a single connect call and then manage the details of the signal in the lambda slot.


  • Lifetime Qt Champion

    You can't have only one connect statement. However with the introspection, you can loop on the signals to connect them to the same lambda.

    However there's one issue that you are going to have: signals will have different parameters, how are you going to manage that in your lambda ?



  • @SGaist , sorry thats really what I meant, I would have a single map in my application where my derived Qt control classes register they're signals and then that map will be processed after reading the configuration from XML files where the subscribers will be defined. The subscribers (slots) will be connected to the signals using a common function. From what I've seen so far, having different parameters isn't a problem.


  • Lifetime Qt Champion

    Some sort of configurable pub/sub design ?



  • @SGaist , absolutely, everything I'm working towards will take Qt into a new era, where once the engine is complete, you will only need to have the same engine for each platform you want to run on, but the actual application will be a series of XML files and JavaScript files.


  • Lifetime Qt Champion

    Then you will have to go with introspection.



  • @SGaist said in Connect Prototype help:

    introspection

    Now thats a new one (for me), had to Wiki it!

    https://en.wikipedia.org/wiki/Type_introspection#C++


Log in to reply