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 add a variable in an other variables name?
QtWS25 Last Chance

how to add a variable in an other variables name?

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++variablenameaddproblem
4 Posts 3 Posters 1.3k 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.
  • G Offline
    G Offline
    GhostWolf
    wrote on 8 Oct 2018, 21:09 last edited by
    #1

    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
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Oct 2018, 21:21 last edited by
      #2

      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 10 Oct 2018, 11:17
      2
      • S SGaist
        8 Oct 2018, 21:21

        Hi,

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

        G Offline
        G Offline
        GhostWolf
        wrote on 10 Oct 2018, 11:17 last edited by
        #3

        @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
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on 11 Oct 2018, 11:01 last edited by Asperamanca 10 Nov 2018, 11:04
          #4

          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
          2

          4/4

          11 Oct 2018, 11:01

          • Login

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