Results 1 to 3 of 3

Thread: Re: QString to variable name?

  1. #1
    Join Date
    Jan 2019
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows

    Default Re: QString to variable name?

    Hello gurus,

    lets say I have this:
    Qt Code:
    1. terminal_db_array["dbname"] = "something"
    3. QString first = 'terminal_db';
    4. QString second = '_array["driver"]';
    To copy to clipboard, switch view to plain text mode 

    and I want to check if the variable exists when i join those two strings together:
    if ({first+second}=="something")
    Last edited by shokarta; 9th January 2020 at 10:31.

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 678 Times in 668 Posts
    Qt products
    Windows Android

    Default Re: QString to variable name?

    What you are asking for is something called "reflection", the ability to retrieve metadata about the variables and types defined in a program. It doesn't exist in C++.

    Here are two links, one a blog post and the other an answer to a stackoverflow question. I do not know if either of these solutions will allow you to retrieve the name of the variable that instantiates a type or just lets you get at the type itself.

    Qt types derived from QObject implement a meta-object system that allows you to identify the types and classnames of QObject instances at runtime. It doesn't permit you to identify the names of the actual C++ variables that represent instances of those classes, but because you can use QObject::setObjectName() to give a unique name to each QObject instance, you could use this feature to give each of your QObject instances the same name as the variable that instantiates it.

    The Java language has reflection built in, through the java.lang.reflect package.
    Last edited by d_stranz; 9th January 2020 at 21:39.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2009
    Brisbane, Australia
    Thanked 1,586 Times in 1,515 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows
    Wiki edits

    Default Re: QString to variable name?

    Depending on the specific requirements you could use a different data structure to achieve this goal without reflection. Essentially move the compile-time item terminal_db_array (and the others like it) into run-time data.

Similar Threads

  1. Replies: 5
    Last Post: 5th December 2015, 12:22
  2. Replies: 2
    Last Post: 11th August 2011, 16:42
  3. Access variable using its name as QString
    By homerun4711 in forum Newbie
    Replies: 3
    Last Post: 22nd December 2010, 10:11
  4. Problem with a dynamicCall and a Qstring variable
    By jokinb in forum Qt Programming
    Replies: 5
    Last Post: 25th October 2010, 09:11
  5. Replies: 4
    Last Post: 31st January 2008, 21:44

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.