Like Tree2Likes
  • 2 Post By brewbuck

use varible for label

This is a discussion on use varible for label within the C++ Programming forums, part of the General Programming Boards category; 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); ...

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

    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
    21,588
    Why don't you use an array, or a std::map or std::unordered_map to map label numbers to labels?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    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
    21,588
    Just use an array or some suitable container. If not, you'll just be stuck until later on.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,348
    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
    289
    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
    2,348
    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
    289
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    The insanity of that impresses me.

    Now go do it using an array
    Crossfire and Elkvis like this.
    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, 02:42 AM
  3. Randomize Varible
    By Hussa in forum C++ Programming
    Replies: 5
    Last Post: 01-10-2003, 09:12 AM
  4. Array Varible
    By Jonh in forum C++ Programming
    Replies: 4
    Last Post: 01-09-2003, 05:31 PM
  5. Varible is different once returned.
    By epoch in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2003, 01:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21