Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the name of a variable ?
Forum Updated to NodeBB v4.3 + New Features

How to get the name of a variable ?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kycho
    wrote on 25 Dec 2018, 09:50 last edited by
    #1

    Is there a way to get the name of a variable?
    I want to make it a function.

    Below is the source code I tried.

        QString hello = "World!";
        qDebug() << getVarName(&hello); // I want result : "hello"
    
    
    #define VAR_TO_STR(name) #name
    QString getVarName(QString *var)
    {
        return VAR_TO_STR(var);
    }
    

    I want code that can get the name and value of a variable at the same time.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrdebug
      wrote on 25 Dec 2018, 09:55 last edited by
      #2

      Sorry, could I ask you why do you need this? Which is the situation that requires this?

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kycho
        wrote on 25 Dec 2018, 10:35 last edited by
        #3

        I asked for convenience and curiosity.
        It is not absolutely necessary.

        Below is the cause of this problem.

        Setting::Setting(QWidget *parent) :
            QDialog(parent),
            ui(new Ui::Dialog)
        {
            ui->setupUi(this);
            settings = new QSettings("Setting.ini",QSettings::IniFormat);
            setDefault(&billacceptorPort,"BillDispensor","COM10");
        }
        void Setting::setDefault(QString *var,QString name,QString val)
        {
            if(settings->value(name).toString()=="")
                settings->setValue(name,val);
            *var = settings->value(name).toString();
        }
        
        

        The setDefault function is simple, but iterative.
        So I was thinking of a way to make it simpler.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 25 Dec 2018, 11:17 last edited by
          #4

          what is iterative inside setDefault function ? It is just one if condition. Did you miss something here ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kycho
            wrote on 25 Dec 2018, 11:26 last edited by
            #5

            The function itself is not iterative, but the function is used repeatedly.

            Even if I use the source that I created, it does not interfere with the operation.

            However, you must use more than 100 'setDefault'.

            I want to minimize the possibility of mistakes because I have little experience with the program.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kycho
              wrote on 25 Dec 2018, 11:29 last edited by
              #6

              Thank you for answer.
              I will do it the before way if there is no way to get the name.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dheerendra
                Qt Champions 2022
                wrote on 25 Dec 2018, 11:34 last edited by
                #7

                No way to get the variable name. Also no use from getting the name of the variable. What matters is the value. You must work on the value rather than the variable name.

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply
                2
                • Z Offline
                  Z Offline
                  zhangjingru1995
                  wrote on 18 Aug 2020, 06:50 last edited by
                  #8

                  QDebug() << QT_STRINGIFY(MainWindowVM);

                  QDebug QDebug(nullptr);
                  
                  QDebug << QT_STRINGIFY(QDebug);
                  
                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved