我一直在使用Qt来制作一些Civ5风格的游戏。我刚刚开始使用Qt,我可能非常低效地编写代码,所以对我的代码感到抱歉。
if(buildings[0][0][0])
ui->r_1_n_1->setStyleSheet("QPushButton {border-image: url"+url_camp_inactive+";} QPushButton:hover { border-image: url"+url_camp_active+";} QPushButton:focus {border-image: url"+url_camp_pressed+";}");
else if(buildings[0][0][1])
ui->r_1_n_1->setStyleSheet("QPushButton {border-image: url"+url_fort_inactive+";} QPushButton:hover { border-image: url"+url_fort_active+";} QPushButton:focus {border-image: url"+url_fort_pressed+";}");
else
ui->r_1_n_1->setStyleSheet("QPushButton {border-image: url"+url_inactive+";} QPushButton:hover { border-image: url"+url_active+";} QPushButton:focus {border-image: url"+url_pressed+";}");
if(buildings[0][1][0])
ui->r_1_n_2->setStyleSheet("QPushButton {border-image: url"+url_camp_inactive+";} QPushButton:hover { border-image: url"+url_camp_active+";} QPushButton:focus {border-image: url"+url_camp_pressed+";}");
else if(buildings[0][1][1])
ui->r_1_n_2->setStyleSheet("QPushButton {border-image: url"+url_fort_inactive+";} QPushButton:hover { border-image: url"+url_fort_active+";} QPushButton:focus {border-image: url"+url_fort_pressed+";}");
else
ui->r_1_n_2->setStyleSheet("QPushButton {border-image: url"+url_inactive+";} QPushButton:hover { border-image: url"+url_active+";} QPushButton:focus {border-image: url"+url_pressed+";}");
if(buildings[0][2][0])
ui->r_1_n_3->setStyleSheet("QPushButton {border-image: url"+url_camp_inactive+";} QPushButton:hover { border-image: url"+url_camp_active+";} QPushButton:focus {border-image: url"+url_camp_pressed+";}");
else if(buildings[0][2][1])
ui->r_1_n_3->setStyleSheet("QPushButton {border-image: url"+url_fort_inactive+";} QPushButton:hover { border-image: url"+url_fort_active+";} QPushButton:focus {border-image: url"+url_fort_pressed+";}");
else
ui->r_1_n_3->setStyleSheet("QPushButton {border-image: url"+url_inactive+";} QPushButton:hover { border-image: url"+url_active+";} QPushButton:focus {border-image: url"+url_pressed+";}");
if(buildings[0][3][0])
ui->r_1_n_4->setStyleSheet("QPushButton {border-image: url"+url_camp_inactive+";} QPushButton:hover { border-image: url"+url_camp_active+";} QPushButton:focus {border-image: url"+url_camp_pressed+";}");
else if(buildings[0][3][1])
ui->r_1_n_4->setStyleSheet("QPushButton {border-image: url"+url_fort_inactive+";} QPushButton:hover { border-image: url"+url_fort_active+";} QPushButton:focus {border-image: url"+url_fort_pressed+";}");
else
ui->r_1_n_4->setStyleSheet("QPushButton {border-image: url"+url_inactive+";} QPushButton:hover { border-image: url"+url_active+";} QPushButton:focus {border-image: url"+url_pressed+";}");
if(buildings[0][4][0])
ui->r_1_n_5->setStyleSheet("QPushButton {border-image: url"+url_camp_inactive+";} QPushButton:hover { border-image: url"+url_camp_active+";} QPushButton:focus {border-image: url"+url_camp_pressed+";}");
else if(buildings[0][4][1])
ui->r_1_n_5->setStyleSheet("QPushButton {border-image: url"+url_fort_inactive+";} QPushButton:hover { border-image: url"+url_fort_active+";} QPushButton:focus {border-image: url"+url_fort_pressed+";}");
else
ui->r_1_n_5->setStyleSheet("QPushButton {border-image: url"+url_inactive+";} QPushButton:hover { border-image: url"+url_active+";} QPushButton:focus {border-image: url"+url_pressed+";}");
我想知道,因为我有大约50个其他的PushButtons名称,r_1_n_(i),因为所有的语句都是相似的,它们可以实现for循环的一些时间,这减少了所需的代码量。所以我想像
这样的东西 for(int j = 0; j < number; i++)
{
for(int i = 0; i < number; i++)
{
if(buildings[j][i][0])
ui->r_(j)_n_(i)->setStyleSheet("QPushButton {border-image: url"+url_camp_inactive+";} QPushButton:hover { border-image: url"+url_camp_active+";} QPushButton:focus {border-image: url"+url_camp_pressed+";}");
}
}
现在显然在做r_(j) n (i)不起作用,但我想知道是否有办法让它发挥作用。感谢。