Is there 'findChild<>()' but for simple variables?



  • Not quite recently i found for myself such nice Qt feature which allows find objects by it's name using QString. Now i faced absolute necessity doing same, but for variables and structs.

    Here is real example. I have 26 different bool variables , named with single letter like A,B,C,D...
    What i want is use some function, which would return pointer to my bool object, specified by const string as a parameter. Is such magic possible in C++, or it's just ability of Qt only for it's own types?


  • Moderators

    I have 26 different bool variables , named with single letter like A,B,C,D...

    That sounds truly horrible.

    Is such magic possible in C++

    C++ is not a dynamic language. It has no built in introspection. You need to build that yourself or use a library (like Qt).

    or it's just ability of Qt only for it's own types?

    Yes and no. It's available for any type, byt you have to make the variable known to the Qt's meta system.
    For classes that's what Q_OBJECT macro does. Among other things it allows you to find and call methods by name.

    For class members in QObject derived classes you can create Q_PROPERTY to find/get/set them by name.


  • Qt Champions 2017

    @Engelard said in Is there 'findChild<>()' but for simple variables?:

    Here is real example. I have 26 different bool variables , named with single letter like A,B,C,D...

    And what stops you from using a hash for this for example, or a vector or w/e?
    There's a distinct difference between types and instances of these types, so while Chris is right I don't believe you really want (or need) introspection.

    That sounds truly horrible.

    Amen, brother! :)



  • @kshegunov said in Is there 'findChild<>()' but for simple variables?:

    And what stops you from using a hash

    I don't know what that is in C++.

    @kshegunov said in Is there 'findChild<>()' but for simple variables?:

    or a vector

    Incoming values to my function are string single letters, it would be needed converter from particular letter to digit, and same reverse then. Thats why i'm asking for findChild analog.

    @kshegunov said in Is there 'findChild<>()' but for simple variables?:

    or w/e?

    ??

    Anyway, question was - 'is it possible' in C++?, now i know the answer.


  • Qt Champions 2017

    @Engelard said in Is there 'findChild<>()' but for simple variables?:

    I don't know what that is in C++.

    Well it's a data structure. In Qt it's called QHash (std::unordered_map in the standard library) and provides mapping between two sets of variables of given types. In your case from strings to bools.



  • the long and short of this issue is that if you want to "find" an object in Qt then you need that object ot be a sublclass of QObject. So, if you want to use it for ordinal/simple/immutable variables then you need to create special classes that wrap those variables and derive from QObject.

    HOWEVER!!!! trying to do this really deontes a poor design and you should reevaluate your reason for doing so.


Log in to reply