我在Windows 7上使用Qt5.在我当前的应用程序中,我有以下代码更改了某些按钮的背景颜色:
...
for(int i = 0; i < layout()->count(); i++)
{
QPushButton * button = static_cast<QPushButton*>(layout()->itemAt(i)->widget());
button->setStyleSheet(backgroundColor);
}
Run Code Online (Sandbox Code Playgroud)
好吧,我有2个关于上述代码的问题:
使用是正确/正确static_cast还是我应该使用其他类型的铸件?
是否可以使用foreach而不是for loop上述?
thu*_*uga 12
您应该使用,qobject_cast以便检查演员表是否成功.如果强制转换失败,则返回0.
QPushButton * button = qobject_cast<QPushButton*>(layout()->itemAt(i)->widget());
if(button)
// cast ok
else
// cast failed
Run Code Online (Sandbox Code Playgroud)
您无法使用,foreach因为您需要一个容器.
| 归档时间: |
|
| 查看次数: |
2676 次 |
| 最近记录: |