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 match an int to predefined variable
Forum Update on Monday, May 27th 2025

How to match an int to predefined variable

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 282 Views
  • 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.
  • D Offline
    D Offline
    deleted286
    wrote on 24 Mar 2021, 13:54 last edited by
    #1

    I have these variable declarations on my mainwindow.h file
    They have integers from 0 to 5.
    Im reading my json file, and assign them this integers.

    int TM_AP_ID_PID;    //it is 0 and  i use it as an idex
    int TM_PAR_ROLL_KP;
    int TM_PAR_ROLL_KI;
    int TM_PAR_ROLL_KD;
    int TM_PAR_PITCH_KP;
    int TM_PAR_PITCH_KI;
    
    

    I have another header file and in there I have these predefines.

    #define TM_AP_ID_PID       	    0   //i used them as an index of variables.   
    #define TM_PAR_ROLL_KP      1
    #define TM_PAR_ROLL_KI        2
    #define TM_PAR_ROLL_KD      3
    #define TM_PAR_PITCH_KP     4
    #define TM_PAR_PITCH_KI      5
    

    I want to match my integer definitions to my predefines.
    How can i do it, is it possible to do?
    I'm open to any idea

    J J 2 Replies Last reply 24 Mar 2021, 14:17
    0
    • D deleted286
      24 Mar 2021, 13:54

      I have these variable declarations on my mainwindow.h file
      They have integers from 0 to 5.
      Im reading my json file, and assign them this integers.

      int TM_AP_ID_PID;    //it is 0 and  i use it as an idex
      int TM_PAR_ROLL_KP;
      int TM_PAR_ROLL_KI;
      int TM_PAR_ROLL_KD;
      int TM_PAR_PITCH_KP;
      int TM_PAR_PITCH_KI;
      
      

      I have another header file and in there I have these predefines.

      #define TM_AP_ID_PID       	    0   //i used them as an index of variables.   
      #define TM_PAR_ROLL_KP      1
      #define TM_PAR_ROLL_KI        2
      #define TM_PAR_ROLL_KD      3
      #define TM_PAR_PITCH_KP     4
      #define TM_PAR_PITCH_KI      5
      

      I want to match my integer definitions to my predefines.
      How can i do it, is it possible to do?
      I'm open to any idea

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 24 Mar 2021, 14:17 last edited by
      #2

      @suslucoder You could use a QMap to map your definitions to actual values:

      QMap<int, int> vars;
      ...
      vars[TM_AP_ID_PID] = 0;
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • N Offline
        N Offline
        nagesh
        wrote on 24 Mar 2021, 14:20 last edited by
        #3

        @suslucoder Use enumerator rather than #define variables

        1 Reply Last reply
        4
        • D deleted286
          24 Mar 2021, 13:54

          I have these variable declarations on my mainwindow.h file
          They have integers from 0 to 5.
          Im reading my json file, and assign them this integers.

          int TM_AP_ID_PID;    //it is 0 and  i use it as an idex
          int TM_PAR_ROLL_KP;
          int TM_PAR_ROLL_KI;
          int TM_PAR_ROLL_KD;
          int TM_PAR_PITCH_KP;
          int TM_PAR_PITCH_KI;
          
          

          I have another header file and in there I have these predefines.

          #define TM_AP_ID_PID       	    0   //i used them as an index of variables.   
          #define TM_PAR_ROLL_KP      1
          #define TM_PAR_ROLL_KI        2
          #define TM_PAR_ROLL_KD      3
          #define TM_PAR_PITCH_KP     4
          #define TM_PAR_PITCH_KI      5
          

          I want to match my integer definitions to my predefines.
          How can i do it, is it possible to do?
          I'm open to any idea

          J Online
          J Online
          JonB
          wrote on 24 Mar 2021, 15:59 last edited by JonB
          #4

          @suslucoder
          It is not clear what you are trying to do here.

          I want to match my integer definitions to my predefines.

          "Match" in what sense?

          You have 6 variables. Does that mean each of those variables can take its own value, independent of whatever is in the others? In that case, it is not clear what the purpose of the #defines is?

          Especially when you write:

          int TM_AP_ID_PID;    //it is 0 and  i use it as an idex
          

          Is the value of TM_AP_ID_PID always 0? Then why do you have it in a variable?

          Purely at a guess, you should be considering one or both of these:

          • Use a C++ enum.

          • Create a 6-element array (or map) for your currently separate variables, so that you can address them via array[TM_something].

          1 Reply Last reply
          1

          1/4

          24 Mar 2021, 13:54

          • Login

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