• 3550阅读
  • 2回复

[提问]findchildren找不到对象的问题 [复制链接]

上一主题 下一主题
离线ddtx
 

只看楼主 倒序阅读 楼主  发表于: 2016-12-14
  1. QList<FlashSectorItem> list = DataManager.GetDeviceSectors();
  2.     FlashSectorItem tempitem;
  3.     int size = list.size();
  4.     qDebug()<<mylayout->count();
  5.     for(int i=0;i<size;i++)
  6.     {
  7.         tempitem = list.at(i);
  8.         QCheckBox *checkbox = new QCheckBox();
  9.         checkbox->setObjectName("checkbox");
  10.         checkbox->setCheckable(true);
  11.         checkbox->setChecked(false);
  12.         checkbox->setText(tempitem.GetSectorName()+":"+QString::number(tempitem.GetSectorStartAddress(),16) +"-"+QString::number(tempitem.GetSectorStartAddress()+tempitem.GetSectroLength()-1,16));
  13.         mylayout->addWidget(checkbox);
  14.     }
  15.     qDebug()<<mylayout->count();
  16.     QList<QCheckBox*> checklist =mylayout->findChildren<QCheckBox*>("checkbox");
  17.     qDebug()<<checklist.size();

如以上代码,我添加了14个qcheckbox,但是下边获取findchildren的时候,返回的LIST为空,请教,有人遇到过类似的问题吗?
从layout的count中可以看到addwidget添加成功了,但是findchildren的时候失败
离线lemonzll

只看该作者 1楼 发表于: 2016-12-14
layout只是用来管理布局的,你添加的QCheckBox的parent还是容纳layout的widget,你需要用widget的findChildren来搜索
离线ddtx

只看该作者 2楼 发表于: 2016-12-14
回 lemonzll 的帖子
lemonzll:layout只是用来管理布局的,你添加的QCheckBox的parent还是容纳layout的widget,你需要用widget的findChildren来搜索(2016-12-14 14:42)嬀/color]

正解。。。我找了好久才发现其父类是窗口而不是layout
快速回复
限100 字节
 
上一个 下一个