c++ - How to get first item as default item in combo box delegate? -


i using qabstracttablemodel , comboboxitemdelegate inherits qstyleditemdelegate. able populate data in combo boxes in table view default nothing displayed after clicking correct data visible in dropdown. how can set first item default item , display it?

my combox delegate:

class comboboxitemdelegate : public qstyleditemdelegate {     q_object public:     comboboxitemdelegate(qobject* parent = 0);     ~comboboxitemdelegate();      virtual qwidget* createeditor(qwidget* parent, const qstyleoptionviewitem& option, const qmodelindex& index) const;     virtual void seteditordata(qwidget* editor, const qmodelindex& index) const;     virtual void setmodeldata(qwidget* editor, qabstractitemmodel* model, const qmodelindex& index) const;     virtual void updateeditorgeometry(qwidget *editor, const qstyleoptionviewitem &option, const qmodelindex &index) const; }; 

create editor function:

qwidget* comboboxitemdelegate::createeditor(qwidget* parent, const qstyleoptionviewitem& option, const qmodelindex& index) const {         qcombobox* cb = new qcombobox(parent);         qstringlist destlist = sfiltereditordata::instance().getdestinationlist();         cb->additem(qstring("all"));         (int = 0; < destlist.size(); i++)         {             cb->additem(destlist.at(i));         }         cb->seteditable(true);         return cb; } 

set editor function:

void comboboxitemdelegate::seteditordata(qwidget* editor, const qmodelindex& index) const {     if (qcombobox* cb = qobject_cast<qcombobox*>(editor)) {         //qstring currenttext = index.data(qt::editrole).tostring();         qstring currenttext = index.model()->data(index, qt::displayrole).tostring();         int cbindex = cb->findtext(currenttext);         // if valid, adjust combobox         if (cbindex >= 0)             cb->setcurrentindex(cbindex);     }     else {         qstyleditemdelegate::seteditordata(editor, index);     } } 

inserting rows in model:

bool stablemodel::insertrows(int position, int rows, const qmodelindex &parent) {     int columns = columncount();     begininsertrows(parent, position, position + rows - 1);      (int row = 0; row < rows; ++row) {         qstringlist items;         (int column = 0; column < columns; ++column)         {             items.append(""); // might reason issue.         }         rowlist.insert(position, items);     }     endinsertrows();     return true; } 

setting data in model:

qvariant stablemodel::data(const qmodelindex &index, int role) const {     if (!index.isvalid())         return qvariant();      if (role == qt::displayrole)         return rowlist[index.row()][index.column()];     else if (role == qt::checkstaterole && index.column() == 0)     {          int status = sfiltereditordata::instance().getstatus(index.row());         if (status)             return qt::checked;         else             return qt::unchecked;     }     else         return qvariant(); }  bool stablemodel::setdata(const qmodelindex &index,                          const qvariant &value, int role) {     if (!index.isvalid() /*|| role != qt::editrole*/)         return false;     if (role == qt::checkstaterole)     {         if ((qt::checkstate)value.toint() == qt::checked)         {             sfiltereditordata::instance().setstatus(index.row(),1);             return true;         }         else         {             sfiltereditordata::instance().setstatus(index.row(), 0);             return true;         }     } 

i have referred link: http://www.qtcentre.org/threads/39194-seteditordata()-for-qcombobox-delegate


Comments