How to edit data in a TableModel/TableView?
-
Hello all,
I have the following code:
@
class CustomerTableModel < Qt::AbstractTableModel
...def flag index return Qt::ItemIsEditable; end def setData index, value, role = Qt::EditRole if (index.isValid() && role == Qt::EditRole) update_value(index.row(), value.toString()) emit dataChanged(index, index) return true end return false end
end
class GUICustomerWidget < Qt::Widget
def initialize
@tableView.set_model(CustomerTableModel.new())
@tableView.setEditTriggers( Qt::AbstractItemView::DoubleClicked )
end
end
@TableView show all data but I can't edit. I'm reading "this page":http://www.qtforum.org/index.php?page=ExternalLink&url=http://qt-project.org/doc/qt-4.8/model-view-programming.html#making-the-model-editable but doesn't work. Should I add something to the tableView?
Can please anyone tell me what I do wrong? -
You should override flags() method from QAbstractItemModel.
@
Qt::ItemFlags Model::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);if (index.isValid()) return Qt::ItemIsEditable | defaultFlags; else return defaultFlags;
}@
Sorry for c++ code))