c++ - How to get QModelIndex to insert a new row in a QAbstractTableModel? -
i using model/view programming first time, using table view along qabstracttablemodel
, qstyleditemdelegate
need combo boxes. model:
class stablemodel : public qabstracttablemodel { q_object public: stablemodel(int rows = 1, int columns = 1, qobject *parent = 0); int rowcount(const qmodelindex &parent = qmodelindex()) const; int columncount(const qmodelindex &parent = qmodelindex()) const; qvariant data(const qmodelindex &index, int role) const; qvariant headerdata(int section, qt::orientation orientation, int role = qt::displayrole) const; qt::itemflags flags(const qmodelindex &index) const; bool setdata(const qmodelindex &index, const qvariant &value, int role = qt::editrole); bool insertrows(int position, int rows, const qmodelindex &parent = qmodelindex()); bool insertcolumns(int position, int columns, const qmodelindex &parent = qmodelindex()); bool removerows(int position, int rows, const qmodelindex &parent = qmodelindex()); bool removecolumns(int position, int columns, const qmodelindex &parent = qmodelindex()); private: qlist<qstringlist> rowlist; comboboxitemdelegate *mydelegate; };
here trying add new row in model , application crashing, think crashing because of invalid index.
stablemodel *model; = new stablemodel(1, 1, this); qmodelindex index = model->index(1, 1, qmodelindex()); model->insertrows(1, 4, index);
how can correct index here or guide me add row using approach. thanks
edit:
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(""); rowlist.insert(position, items); } endinsertrows(); return true; }
the model->index(1, 1, qmodelindex());
returns invalid index because there no index coordinates [1, 1].
can remove line, don't need parent index table.
with provided information, can't why index()
method crashes. shouldn't, if invalid row , column number provided.
Comments
Post a Comment