I am using Qt designer to create a set of 32 status "lights." My lights are QLabels. So initialize my blank Qlabel to have a yellow style sheet and then i set each light to red or green based upon the text file input of Boolean data. My problem is that each time I have to hard code it because the designer syntax is ui->color_0->setStyleSheet("background-color: rgb(0, 255, 0);");
color_0, color_1, etc are QLabel objects. I wanted to write a for loop and concatenate (append) the loop incrementer to color but that does not work because it will not be of type QLabel. Code below, let me know how you would clean up this code and make it more efficient.
void static2::on_pushButtonNext_clicked()
{
if (incrementer == 0)
{
int tot_size = text.size();
const char *str;
QByteArray array; //http://www.qtcentre.org/threads/22711-Converting-QString-to-char-array
array = text.toLatin1();
str = array.data();
}
//write an if statement here that prevents from running past the total stream. run if less than num_events_dec to end.
if (incrementer*num_events_dec <= tot_size - num_events_dec)
{
incrementer++;
ui->lcdNumber->display(incrementer); //updates display
int step = (incrementer-1)*num_events_dec; //this is the code that goes bit by bit on the stream
for (int i = step; i < step+num_events_dec; i++){
//PLACE COLOR SETTING COMMANDS HERE
//http://stackoverflow.com/questions/2749798/qlabel-set-color-of-text-and-background
//http://www.qtcentre.org/archive/index.php/t-5944.html
if (str[i] == '1'){
//make label background green
//qDebug() << "high";
//QLabel* color = new QLabel; //[num_events_dec]; //allocates an array of objects called color which is of the user specified size
/*QString color = "color_" + QString::number(i);
qDebug() << i;
qDebug() << color;
*/
/* As of right now i have string values that are of the proper name
* I need those same names as Qlabels in order for ui->xxx to recognize them
* The class mainwindow has an object ui which has an object called color_[i]
*/
//ui->color->setStyleSheet("background-color: rgb(0, 255, 0);");
switch (i-step){
case 0: ui->color_0->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 1: ui->color_1->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 2: ui->color_2->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 3: ui->color_3->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 4: ui->color_4->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 5: ui->color_5->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 6: ui->color_6->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 7: ui->color_7->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 8: ui->color_8->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 9: ui->color_9->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 10: ui->color_10->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 11: ui->color_11->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 12: ui->color_12->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 13: ui->color_13->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 14: ui->color_14->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 15: ui->color_15->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 16: ui->color_16->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 17: ui->color_17->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 18: ui->color_18->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 19: ui->color_19->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 20: ui->color_20->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 21: ui->color_21->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 22: ui->color_22->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 23: ui->color_23->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 24: ui->color_24->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 25: ui->color_25->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 26: ui->color_26->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 27: ui->color_27->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 28: ui->color_28->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 29: ui->color_29->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 30: ui->color_30->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
case 31: ui->color_31->setStyleSheet("background-color: rgb(0, 255, 0);"); break;
} //end switch case
} //end if
else if (str[i] == '0'){
//make label background red
//qDebug() << "low";
switch (i-step){
case 0: ui->color_0->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 1: ui->color_1->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 2: ui->color_2->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 3: ui->color_3->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 4: ui->color_4->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 5: ui->color_5->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 6: ui->color_6->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 7: ui->color_7->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 8: ui->color_8->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 9: ui->color_9->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 10: ui->color_10->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 11: ui->color_11->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 12: ui->color_12->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 13: ui->color_13->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 14: ui->color_14->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 15: ui->color_15->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 16: ui->color_16->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 17: ui->color_17->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 18: ui->color_18->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 19: ui->color_19->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 20: ui->color_20->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 21: ui->color_21->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 22: ui->color_22->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 23: ui->color_23->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 24: ui->color_24->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 25: ui->color_25->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 26: ui->color_26->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 27: ui->color_27->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 28: ui->color_28->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 29: ui->color_29->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 30: ui->color_30->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
case 31: ui->color_31->setStyleSheet("background-color: rgb(255, 0, 0);"); break;
} //end switch case
} //end else if
else {
QMessageBox::warning(this,"Error", "A non-binary number has been read -- Check input stream");
} //end else
} //end for loop
} //end if
else //dont allow to increment
{
QMessageBox::warning(this,"Error", "Stream Ended, No more data to view");
} //end else
} //end push button click