我有一堆名爲「e1」,「e2」,「e3」等的lineEdits。 我想設置相同的樣式表對所有這些在一個循環中,沒有實現代碼QT/C++通過從變量中獲取其名稱來設置QObject的屬性
ui->e1->setStyleSheet("background-color: white");
ui->e2->setStyleSheet("background-color: white");
ui->e3->setStyleSheet("background-color: white");
ui->e4->setStyleSheet("background-color: white");
ui->e5->setStyleSheet("background-color: white");
ui->e6->setStyleSheet("background-color: white");
,但這樣的事情:
for (z=1; z<7; z=z+1)
{ui->e&z->setStyleSheet("background-color: white");}
也許有另一種方式的屬性設置爲一堆相似的物體?任何幫助表示感謝,提前謝謝!
一些原因我不得不使用Qt Disigner;(和事業恰克lineEdits的樣式表是使lineEdit的背景紅色以防輸入錯誤的數據,所以這段代碼是從清除背景到「默認」白色的按鈕。 –
有狀態的樣式?[你可以用動態屬性來做到這一點](http://doc.qt.io/ qt-5/stylesheet-examples.html#customizing-using-dynamic-properties)。在HTML/CSS中的等價物將是使用'.class-selectors',但儘管Qt樣式表存在這種語法,但它意味着不同的東西。 – JesseTG