私はQtデザイナを使用して32個のステータス "ライト"のセットを作成しています。私のライトはQLabelsです。空のQlabelを初期化して黄色のスタイルシートを作成し、ブールデータのテキストファイル入力に基づいて各ライトを赤または緑に設定します。私の問題は、デザイナ構文がui-> color_0-> setStyleSheet( "background-color:rgb(0、255、0);")であるため、Qtデザイナ - スタイルシートを設定 - ハードコードされました - プログラム的に設定したい
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を渡すことはできません。 – mjordan290