1
在for循環中,我可以構造一個字符串。通過字符串獲取小部件
foreach (...) {
QString str = "pushButton" + QString::number(count);
// this gives pushButton1
// Now I want to get the button widget that has this string as it's variable name
// and modify it in some way, e.g. change it's button caption
str->setText("New Title");
count ++;
}
這可能嗎?如果是這樣,那麼如何
編輯:以下是我創建的按鈕
for (int i=0; i<total; i++) {
QPushButton *btn = new QPushButton();
btn->setObjectName("pushButton" + QString::number(i));
ui->horizontalLayout->addWidget(btn);
}
您是如何創建按鈕的? – eyllanesc
Hi @eyllanesc。我在循環中以編程方式創建它,並用循環變量分配它們的名稱。 – GeneCode
您是否已將父級傳遞給按鈕? – eyllanesc