【发布时间】:2017-04-27 19:41:28
【问题描述】:
我正在使用 Qt 设计器创建一组 32 个状态“灯”。我的灯是 QLabels。因此,将我的空白 Qlabel 初始化为具有黄色样式表,然后根据布尔数据的文本文件输入将每个灯设置为红色或绿色。我的问题是每次我都必须对其进行硬编码,因为设计器语法是 ui->color_0->setStyleSheet("background-color: rgb(0, 255, 0);");
color_0、color_1 等是 QLabel 对象。我想编写一个 for 循环并将循环增量器连接(附加)到颜色,但这不起作用,因为它不是 QLabel 类型。下面的代码,让我知道您将如何清理此代码并使其更高效。
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
} //结束按钮点击
【问题讨论】:
-
为什么不直接创建一个带有 2 个参数 qlabel* 和 qcolor 的函数。
-
我会不会遇到和以前一样的问题,我必须传递诸如 color_xxx 之类的对象???如果我在函数中需要它成为 QLabel,我不相信我可以传递一个 QString color_xxx。
标签: c++ qt qt-creator qt-designer