Thread: use varible for label

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    307

    use varible for label

    ok how do i turn this...
    Code:
        ui->label_2->setVisible(true);
        ui->label_3->setVisible(false);
        ui->label_4->setVisible(true);
        ui->label_5->setVisible(false);
        ui->label_6->setVisible(true);
        ui->label_7->setVisible(false);
        ui->label_8->setVisible(true);
        ui->label_9->setVisible(false);
        ui->label_10->setVisible(true);
        ui->label_11->setVisible(false);
        ui->label_12->setVisible(true);
        ui->label_13->setVisible(false);
        ui->label_14->setVisible(true);
        ui->label_15->setVisible(false);
    into this
    Code:
    for (x=0;x<16;x++)
          {           
               ui->label_(X)->setVisible(true);
               ui->label_(X+1)->setVisible(false);
          }
    need to have the name ("label_" + X)everything i know i tried, i have 641 lables that change states and i dont want to have to "manualy" switch each onebeing able to make a function for it would be the best!Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why don't you use an array, or a std::map or std::unordered_map to map label numbers to labels?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    was thinking of using an array later on, but atm, just stuck trying to use a variable in the "ui->label_" part!

    forgot to mention, if it makes a difference i am using QT!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just use an array or some suitable container. If not, you'll just be stuck until later on.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    there is no way to dynamically do exactly what you want at run time. at best, you could write a code generator program, but like laserlight says, use a vector or an array or some other suitable container.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    yeah wrote a c program to make my c++ program! lol

    just figured there was an easier way to control visible true/false with a variable for the name itself.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Crossfire View Post
    yeah wrote a c program to make my c++ program!
    I've done exactly that. I have a program that generates one class per table in a mysql database, and writes one header with the class definitions and one source file and the member function implementations. it takes about 30 seconds to run for my production database, when doing it by hand would take many many hours, and is far less error-prone than hand-writing queries for every time I want to perform a database operation. it's a lot of fun writing code that writes code.

    a std::unordered_map would probably be the best solution. if you don't have access to a C++11 compiler yet, you can use std::map.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    yeah for all 320 pushbuttons, i did this ran it buttonmaker.exe > buttonmaker.txt then just copied and pasted!!!

    Code:
    #include<stdio.h>
    #include<stddef.h>
    #include<stdlib.h>
    int main(void)
    {
     int X,on,off,pushbutton,relay,end;
     
     
     
     printf("Enter PushButton Number to start: ");
     scanf("%i",&X);
     printf("Enter PushButton Number to end: ");
     scanf("%i",&end);
     printf("\n\n\n");
     for (pushbutton=X;pushbutton<=end;pushbutton++)
     {
     
     relay = pushbutton-1;
     off = relay*2;
     
     on = off+1;
     
     printf("void RelayPanel::on_pushButton_%i_clicked()\n",pushbutton);
     printf("{\n");
     printf("\tToggleRelay(%i);\n",relay);
        printf("\tif (Status == 0)\n");
        printf("\t\t{\n");
     printf("\t\t\tui->label_%i->setVisible(true);\n",off);
     printf("\t\t\tui->label_%i->setVisible(false);\n",on);
     printf("\t\t}\n");
     printf("\telse\n");
        printf("\t\t{\n");
     printf("\t\t\tui->label_%i->setVisible(false);\n",off);
     printf("\t\t\tui->label_%i->setVisible(true);\n",on);
     printf("\t\t}\n");
     printf("}\n\n");
    }

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The insanity of that impresses me.

    Now go do it using an array
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STATIC text label or LABEL in Windows APP - NO MFC!
    By triste in forum Windows Programming
    Replies: 5
    Last Post: 10-25-2004, 11:14 PM
  2. what type of varible is for HEX
    By megablue in forum C Programming
    Replies: 1
    Last Post: 11-20-2003, 03:42 AM
  3. Randomize Varible
    By Hussa in forum C++ Programming
    Replies: 5
    Last Post: 01-10-2003, 10:12 AM
  4. Array Varible
    By Jonh in forum C++ Programming
    Replies: 4
    Last Post: 01-09-2003, 06:31 PM
  5. Varible is different once returned.
    By epoch in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2003, 02:45 AM