查看完整版本: [-- findchildren找不到对象的问题 --]

QTCN开发网 -> 其它技术开发讨论区 -> findchildren找不到对象的问题 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

ddtx 2016-12-14 13:46

findchildren找不到对象的问题

  1. QList<FlashSectorItem> list = DataManager.GetDeviceSectors();
        FlashSectorItem tempitem;
        int size = list.size();
        qDebug()<<mylayout->count();
        for(int i=0;i<size;i++)
        {
            tempitem = list.at(i);
            QCheckBox *checkbox = new QCheckBox();
            checkbox->setObjectName("checkbox");
            checkbox->setCheckable(true);
            checkbox->setChecked(false);
            checkbox->setText(tempitem.GetSectorName()+":"+QString::number(tempitem.GetSectorStartAddress(),16) +"-"+QString::number(tempitem.GetSectorStartAddress()+tempitem.GetSectroLength()-1,16));
            mylayout->addWidget(checkbox);
        }
        qDebug()<<mylayout->count();
        QList<QCheckBox*> checklist =mylayout->findChildren<QCheckBox*>("checkbox");
        qDebug()<<checklist.size();

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

lemonzll 2016-12-14 14:42
layout只是用来管理布局的,你添加的QCheckBox的parent还是容纳layout的widget,你需要用widget的findChildren来搜索

ddtx 2016-12-14 17:06
lemonzll:layout只是用来管理布局的,你添加的QCheckBox的parent还是容纳layout的widget,你需要用widget的findChildren来搜索(2016-12-14 14:42)嬀/color]

正解。。。我找了好久才发现其父类是窗口而不是layout


查看完整版本: [-- findchildren找不到对象的问题 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled