c++ - Do an action while any checkbox state is modified with Qt -


in programm, fill interface lot of checkbox way :

void vgccc::addmaterialtoui(qdomnodelist _materialnodelist, qwidget* _areawidget, qlayout* _layout, qwidget* _layoutwidget, int _matable) {     for(int i=0; i< _materialnodelist.count();i++)     {         qdomelement materialelement = _materialnodelist.at(i).toelement();         qstring elementfile = materialelement.attribute("file");         qstring elementid = materialelement.attribute("id");         qstring elementlabel = elementid;         elementlabel += "  -  ";         elementlabel += materialelement.attribute("label");         qcheckbox* checkbox = new qcheckbox(elementlabel);         _layout->addwidget(checkbox);         _layoutwidget->adjustsize();         _areawidget->setminimumheight(_layoutwidget->height());         _areawidget->setminimumwidth(_layoutwidget->width());          configuration c;         c.path = (m_igmpath+elementfile).tostdstring();         c.id = elementid.toint();         c.name = elementlabel.tostdstring();         if(_matable==0)         {             m_materialsectionmap[checkbox] = c;         }         else         {             m_materialpostmap[checkbox] = c;         }     } } 

i know how retrieve these "abstract" checkbox. more exactly, if 1 of these checkbox checked, call function :

    connect(anycheckbox,signal(statechanged(anycheckbox)), this, slot(dosomethingfunctionifcheckboxischecked())); 

the difficulty in ui, these checkbox didn't exist, can't connect them function. how can solve ?

you can e.g. collect pointers checkbox objects list can access or "retrieve" them later.

you can connect each checkbox's statechanged signal same slot called when state of of checkboxes changed. in slot can cast sender() checkbox if need know specific checkbox in question. alternative use qsignalmapper.

in class declaration:

private slots:     void checkboxstatechanged(int state)          private:     qlist<qcheckbox*> m_checkboxes; 

in class definition:

void vgccc::addmaterialtoui(qdomnodelist _materialnodelist, qwidget* _areawidget, qlayout* _layout, qwidget* _layoutwidget, int _matable) {     ...     qcheckbox* checkbox = new qcheckbox(elementlabel);     m_checkboxes.append(checkbox);     connect(checkbox, signal(statechanged(int)), this, slot(checkboxstatechanged(int)));     ... }  void vgccc::checkboxstatechanged(int state) {     // here can e.g. call dosomethingfunctionifcheckboxischecked()      qcheckbox* checkbox = qobject_cast<qcheckbox*>(sender());     if (checkbox)     {         // checkbox points object state changed     }  }