c++ - Refreshing the choices in a QComboBox used as an editor in a QTableView -
i'm using qcomboxbox
in qtableview
in qt. when add new row createeditor()
of delegate called , can instantiate combobox correct set of choices available @ time. problem is, user can load different files outside table, , based on content of file combobox need updated items. there way can editor of cell, can update choices accordingly? since other cells of table should not destroyed, can not recreate table new data, need update comboboxes of cells.
i've been looking in sourcecode of qabstractitemview
, there function editorforindex()
need, implemented privately inside view not accessible in derived class. of course can keep record of boxes create, can update them accordingly later on, wonder if there no other way of doing this.
you can have contents of combobox class member of delegate in qstringlist
. item delegate can :
#include <qitemdelegate> #include <qcombobox> class comboboxdelegate: public qitemdelegate { q_object public: comboboxdelegate(qobject *parent = 0); qwidget *createeditor( qwidget *parent, const qstyleoptionviewitem &option, const qmodelindex &index ) const; void seteditordata( qwidget *editor, const qmodelindex &index ) const; void setmodeldata( qwidget *editor, qabstractitemmodel *model, const qmodelindex &index ) const; void updateeditorgeometry( qwidget *editor, const qstyleoptionviewitem &option, const qmodelindex &index ) const; qstringlist comboitems; mutable qcombobox *combo; private slots: void setdata(int val); }; comboboxdelegate::comboboxdelegate(qobject *parent ):qitemdelegate(parent) { } qwidget *comboboxdelegate::createeditor(qwidget *parent, const qstyleoptionviewitem &option, const qmodelindex &index) const { combo = new qcombobox( parent ); qobject::connect(combo,signal(currentindexchanged(int)),this,slot(setdata(int))); combo->additems(comboitems); combo->setmaxvisibleitems(comboitems.count()); return combo; } void comboboxdelegate::seteditordata(qwidget *editor, const qmodelindex &index) const { qstring text = index.model()->data( index, qt::displayrole ).tostring(); int comboindex = comboitems.indexof(qregexp(text)); if(comboindex>=0) (static_cast<qcombobox*>( editor ))->setcurrentindex(comboindex); } void comboboxdelegate::setmodeldata(qwidget *editor, qabstractitemmodel *model, const qmodelindex &index) const { model->setdata( index, static_cast<qcombobox*>( editor )->currenttext() ); } void comboboxdelegate::updateeditorgeometry(qwidget *editor, const qstyleoptionviewitem &option, const qmodelindex &index) const { editor->setgeometry( option.rect ); } void comboboxdelegate::setdata(int val) { emit commitdata(combo); //emit closeeditor(combo); }
when want update items in combobox somewhere in code pointer item delegate calling itemdelegateforcolumn
, access comboitems
member :
comboboxdelegate * itemdelegate = (comboboxdelegate *)ui->tableview->itemdelegateforcolumn(columnindex); //updating combobox items itemdelegate->comboitems.append("newitem"); ...
Comments
Post a Comment