QObjectList list = toolBar->children();
QObjectList::Iterator it;
for (it = list.begin(); it != list.end(); ++it)
{
QAction *act =qobject_cast<QAction *>(*it) ;
act->setChecked(false);
}
上代码中QAction *act 为NULL?而且发面List中有6个元素,但我明明只加了3个QAction
------最佳解决方案--------------------
不好意思, 上面回答有些漏洞, 应该是在要对QToolBar上的控件进行样式修改或其它操作时, 才通过children()来遍历;
对于只想获取QAction的话, 直接调用actions返回即可~``
------其他解决方案--------------------
QToolBar继承自QWidet,QWidget有actions()方法
------其他解决方案--------------------
注意, 一般要对QToolBar子控件操作, 应该遍历QWidget(通常情况为QToolButton), 而不是QAction~``
QObjectList list = ui.toolBar->children();
QObjectList::Iterator it;
for (it = list.begin(); it != list.end(); ++it)
{
QToolButton* act = qobject_cast<QToolButton *>(*it);
if(act != NULL)
{
qDebug() << act->objectName();
act->setCheckable(true); // 这只是举个例子, 以便看到效果 :)
act->setChecked(true);
}
}
------其他解决方案--------------------
QToolButton不能直接转换成QAction,需要使用方法defaultAction()
------其他解决方案--------------------
嗯, 不过作为要通过QToolBar获取得用QToolButton而已 :) 获取到QToolButton后对QAction的操作只是状态的变化了~``
------其他解决方案--------------------
大哥,toolbar哪有actions方法!!!
------其他解决方案--------------------
qDebug() << act->objectName(); 这个读出来的值怎么是空的??