遍历Qt界面上所有QPushButton

岁小草 2012-07-18 09:55:11
比如,一个界面上有50个QPushButton,每一个button都要调用一个

void setButton(QPushButton *button)的函数。

我想知道有没有一个方法可以遍历界面上的所有button,然后我通过这个迭代

来执行setButton(button)操作。

谢谢!
...全文
1453 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
goodlinux 2013-06-20
  • 打赏
  • 举报
回复
感谢楼上的总结
CanDy_O 2012-07-18
  • 打赏
  • 举报
回复 2
QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
QList<QPushButton *> allPButtons = parentWidget.findChildren<QPushButton *>();
jdwx 2012-07-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

用const QObjectList & QObject::children () const,对列表的每个对象做QPushButton强制转换,成功就可以调用setButton函数。
[/Quote]
tubo_true 2012-07-18
  • 打赏
  • 举报
回复
QObject::children ()
slientman 2012-07-18
  • 打赏
  • 举报
回复
用const QObjectList & QObject::children () const,对列表的每个对象做QPushButton强制转换,成功就可以调用setButton函数。
岁小草 2012-07-18
  • 打赏
  • 举报
回复
O.O 搞定!
/*遍历界面上所有button*/
QList<QPushButton *> button = this->findChildren<QPushButton *>();
qDebug() << button.count();
for(int i=0; i < button.count(); i++)//通过i的范围确定需要设置哪些button
{
setDynamicImage(button.at(i));
}

谢谢楼上各位喽!
我是学友 2012-07-18
  • 打赏
  • 举报
回复
QVector<QPushButton *> yourBt;
也是可以的

16,211

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧