[Qt-interest] creating variable name from string

andrew.m.goth at l-3com.com andrew.m.goth at l-3com.com
Thu Feb 12 18:31:34 CET 2009


ami guru wrote:
> Is there any way to create variable name from QString .
> 
> I have a string list and i want to create variable names out 
> of the string list in runtime.
> 
> The type of those variables are registered within Qt with  
> call Q_DECLARE_METATYPE.

You cannot programmatically create a "variable" (in the sense that the C
or C++ languages define "variable"), but you certainly can
programmatically create an entry in a QMap.  Make a QMap that maps from
names to QVariant values.  It sounds to me like this is what you need.

If the variables already exist and you would like to programmatically
find and modify them, you will have to statically create a mapping from
names to pointers to the variables.  You do this with a QMap or a
C-style array of structs.  You might want to create a preprocessor macro
to simplify the process of populating the mapping.  If a variable isn't
statically placed in the map, it won't be possible to find it at
runtime.

C++ lacks the necessary introspection capabilities to do any of this
without significant help from the programmer.  However, in theory you
can get memory addresses of symbols by having your process dlopen()
itself and get type information by parsing the program's own DWARF
debugger data.  But I recommend against going through this trouble
unless you have a library to help you.  If you find such a library, let
me know. ;^)  I have to do this sort of introspection, and it's tough.

-- 
Andy Goth
<amgoth at link.com>




More information about the Qt-interest-old mailing list