PDA

View Full Version : Can't edit my QTableView cells



MattPhillips
1st February 2011, 15:21
Hi,

1) I have setEditTriggers(QAbstractItemView::AllEditTriggers ) in the constructor of my derived QTableView class (which shouldn't be necessary), and

2) I know that basic signals/slots are working since on_MyTable_activated(const QModelIndex& idx) works and idx does refer to the index of the cell I clicked on, and

3) MyTable->model()->data(idx, Qt::EditRole) returns the actual contents of the cell.

Yet no amount of clicking, keypresses, etc. will allow me to edit the cells, anyone know what's going on? NB model() is in this case a QSortFilterProxyModel, but that doesn't seem like it should make a difference...

Matt

franz
1st February 2011, 15:55
Check if the item is actually editable: (MyTable->model()->flags(idx) & Qt::ItemIsEditable). If not, make it so.

MattPhillips
2nd February 2011, 03:53
franz,

Thank you! That was it. Specifically I needed to reimplement flags() as follows:



Qt::ItemFlags MyTableModel::flags (const QModelIndex &index) const
{
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

I guess I hadn't expected this to be a property of the model, but rather the view, since you could conceivably want an item editable in one view but not editable in another. Anyway, thanks--

Matt

franz
2nd February 2011, 08:38
I guess I hadn't expected this to be a property of the model, but rather the view, since you could conceivably want an item editable in one view but not editable in another.Hmm, yes that could be the case. However, the model specifies whether an item is inherently editable. If you would want to block certain views from editing the item you could always use either a proxy model that strips the editable flag, or use a delegate that doesn't allow editing. In any case, the model/view framework has been designed in a way that changes through one view are (practically) immediately reflected in all other views. And well, if you run into such a situation, maybe it is time to reconsider your design (which is a good thing to do in any case)...

MattPhillips
2nd February 2011, 13:49
Thanks, I will keep those other options in mind.

Matt