c++ - QItemDelegate with custom widgets -
i'm having problems qtableview
, qitemdelegate
classes. 1 column delegate creates simple combo box , works fine. 2nd column need widget has 2 combo boxes in single widget.
i've written following code in qitemdelegate
, clear shows code 2nd column, 1 doesn't work. other simple combo-box isn't shown works fine:
qwidget *userdefinedunitsdelegate::createeditor(qwidget *parent,const qstyleoptionviewitem & option ,const qmodelindex & index ) const { //set simple widget layout qwidget* pwidget = new qwidget(parent); qhboxlayout* hlayout = new qhboxlayout(pwidget); pwidget->setlayout(hlayout); //add 2 combo boxes layout qcombobox* comboeditor = new qcombobox(pwidget); qcombobox* comboeditor2 = new qcombobox(pwidget); //now add both editors hlayout->addwidget(comboeditor); hlayout->addwidget(comboeditor2); return pwidget; }
now displays fine when edit , click elsewhere doesn't stop editing. can offer pointers?
edit: need call commitdata() , closeeditor() @ point. can offer pointers on call these?
thanks.
you can keep editor widget member of class , emit commitdata when current index of 1 of comboboxes has changed. can connect currentindexchanged(int) slot , emit commitdata there:
qwidget *userdefinedunitsdelegate::createeditor(qwidget *parent,const qstyleoptionviewitem & option ,const qmodelindex & index ) const { //set simple widget layout pwidget = new qwidget(parent); qhboxlayout* hlayout = new qhboxlayout(pwidget); pwidget->setlayout(hlayout); //add 2 combo boxes layout qcombobox* comboeditor = new qcombobox(pwidget); qcombobox* comboeditor2 = new qcombobox(pwidget); connect(comboeditor,signal(currentindexchanged(int)),this,slot(setdata(int))); connect(comboeditor2,signal(currentindexchanged(int)),this,slot(setdata(int))); //now add both editors hlayout->addwidget(comboeditor); hlayout->addwidget(comboeditor2); return pwidget; } void userdefinedunitsdelegate::setdata(int val) { emit commitdata(pwidget); }
Comments
Post a Comment