Qt Forum

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

    Unsolved how to add a variable in an other variables name?

    General and Desktop
    c++ variable name add problem
    3
    4
    812
    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.
    • G
      GhostWolf last edited by

      Hello,

      I am trying to make a program, where i have 10 names, and 10 results, but i have 50 things to choose from.
      You have to select a checkbox to add that thing to a name and a result.
      Now i am trying to find out how ta add a number to a variables name, so i dont have to make a whole lot of code.
      this is my example:

      {    qDebug ()<<"start";
           counter=1;
           if (ui->checkBox->isChecked()){
                name+(counter) = "first name";
                result+(counter) = "first result";
                counter++;
           }     
           if (ui->checkBox_2->isChecked()){
               name+(counter) = "second name";
               result+(counter) = "second result";
               counter++;
          }
      qDebug()<<"first name  = "<<name1;
      qDebug()<<"first result = "<<result1;
      qDebug()<<"second name = "<<name2;
      qDebug()<<"second result = "<<result2;
      

      So if you click checkbox 1, the string of the first name has to go to name1, same with result.
      if i click only checkbox 2, then the string of the second name has to go to name1.
      if i click them both, then the string of the first name has to go to name1, and of the second name has to go to name2.
      Is there an easy way to do this?

      (i hope you understand what i mean, it is a bit difficult to explain for me.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        You can't do that with C++. But from the looks of it, a QMap would likely do the job.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        G 1 Reply Last reply Reply Quote 2
        • G
          GhostWolf @SGaist last edited by

          @SGaist said in how to add a variable in an other variables name?:

          Hi,

          You can't do that with C++. But from the looks of it, a QMap would likely do the job.

          Thanks for your answer, I will do it the "hard" way, just code it one for one, might be easier to handle the results as qmap I think, because I have no experience with that.

          1 Reply Last reply Reply Quote 0
          • A
            Asperamanca last edited by Asperamanca

            Fill all results of checked check boxes into a list, and then take the first n entries from that list (up to the number you need, or the size of the list)
            EDIT: If you put the checkboxes into a container, you just have to loop over it

            1 Reply Last reply Reply Quote 2
            • First post
              Last post