Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - Disable editing of cell

Tags:

c++

qt

I have a QTableView (model based) and I want to disable the editing capability of a particular cell, let's say row 0, column 1.

How can I do this? Please note that I still want other cells in this row enabled for editing.

like image 911
boxofapps Avatar asked Sep 21 '12 06:09

boxofapps


1 Answers

If you are using a custom table model, you can implement the Qt::ItemFlags QAbstractItemModel::flags ( const QModelIndex & index ) const method and return a set of flags where the Qt::ItemIsEditable flag is not set for the cells you do not want to edit. Suppose MyTableModel is inherited from QAbstractTableModel:

Qt::ItemFlags MyTableModel::flags ( const QModelIndex & index ) const {
    Qt::ItemFlags flags = Qt::NoItemFlags;

    if (index.row() == 0 && index.column() == 1) {
       return flags;
    }
    return flags | Qt::ItemIsEditable;
}
like image 143
Andreas Fester Avatar answered Sep 28 '22 11:09

Andreas Fester