Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved How to match an int to predefined variable

    General and Desktop
    4
    4
    105
    Loading More Posts
    • 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
      deleted286 last edited by

      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

      jsulm JonB 2 Replies Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @deleted286 last edited by

        @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 Reply Quote 3
        • nagesh
          nagesh last edited by

          @suslucoder Use enumerator rather than #define variables

          1 Reply Last reply Reply Quote 4
          • JonB
            JonB @deleted286 last edited by JonB

            @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 Reply Quote 1
            • First post
              Last post