2016-05-15 50 views
0

QTが新機能です。私は配列内に35個のpush_buttonを持っています。私はボタンのこの配列のための単一の関数を記述したい。その関数はボタンごとに異なる文字列のみを設定します。 "15,15"のような "10,10"のようなものです。 私は理解できるシグナルマッパーを見ました。QTでプッシュボタンの配列を単一の機能にする

シンプルで簡単なアイデアです。

for (int i = 0; i < 35;i++){ 
    PB << new QPushButton(); 
} 

ui->setupUi(this); 

PB[0]=ui->pB_00;PB[1]=ui->pB_01;PB[2]=ui->pB_02;PB[3]=ui->pB_03; 
PB[4]=ui->pB_04;PB[5]=ui->pB_05;PB[6]=ui->pB_06;PB[7]=ui->pB_07; 
PB[8]=ui->pB_08;PB[9]=ui->pB_09;PB[10]=ui->pB_10;PB[11]=ui->pB_11; 
PB[12]=ui->pB_12;PB[13]=ui->pB_13;PB[14]=ui->pB_14;PB[15]=ui->pB_15; 
PB[16]=ui->pB_16;PB[17]=ui->pB_17;PB[18]=ui->pB_18;PB[19]=ui->pB_19; 
PB[20]=ui->pB_20;PB[21]=ui->pB_21;PB[22]=ui->pB_22;PB[23]=ui->pB_23; 
PB[24]=ui->pB_24;PB[25]=ui->pB_25;PB[26]=ui->pB_26;PB[27]=ui->pB_27; 
PB[28]=ui->pB_28;PB[29]=ui->pB_29;PB[30]=ui->pB_30;PB[31]=ui->pB_31; 
PB[32]=ui->pB_32;PB[33]=ui->pB_33;PB[34]=ui->pB_34; 
+0

インラインコードの代わりにコードブロックを使用してください – tmthydvnprt

+0

既存の 'ui->'ボタンを配列にマップしたいだけですか? – DaveyLaser

答えて

0

2つのパラメータのみを使用する関数を追加できます。最初のパラメータは、どのボタンにアクセスするかです。次は設定するテキストになります。 例:

void Parent::changeText(int button, QString text) 
{ 
    PB[button].setText(text); 
} 

複数のボタンを繰り返すことができます。

0

ボタンがすでにui要素に存在する場合、現在のループではnew QPushButtonsを作成する必要はありません。デザイナーを使ってボタンを挿入するのではなく、コードで行うことができます。

// Your button array, this is likely a private member in your class. 
QList< QPushButton* > PB; 

for(auto i = 0; i < 35; ++i) 
{ 
    // Create new button with textValue == "i" 
    auto button = new QPushButton{ QString::number(i) }; 

    // Store in your list 
    PB << button; 

    // Add to ui 
    ui->containerQWidget->layout()->addWidget(button); 
} 
+0

しかし、どのようにすべてのボタンのための単一の機能を持つ各ボタンのための異なる値を持つ単一の文字列変数を設定するには? –

+0

1弦、35ボタン?または35文字列と35ボタン? – DaveyLaser

+0

または...ルックアップテーブル...? 'std :: map '? – DaveyLaser

関連する問題