Vishal Kotcherlakota
Vishal Kotcherlakota

Reputation: 1154

How would I use a QStyledItemDelegate to make only part of a view editable?

I'm trying to teach myself how to use the QStyledItemDelegate class correctly. Qt's got a fantastic example that I read here: Qt Spin Box Delegate Example.

But here's a question I can't seem to answer. Let's take a look at their example screenshot.

Qt Spin Box Delegate Screenshot

How would I write SpinBoxDelegate in their example such that I could only edit part of the data, say, only items in column 2?

Upvotes: 1

Views: 1441

Answers (2)

StefanQ
StefanQ

Reputation: 784

The regular way to indicate that certain parts of a model are not editable is to reimplement QAbstractItemModel::flags() in your model to exclude the Qt::ItemIsEditable flag.

Upvotes: 0

Chris
Chris

Reputation: 17535

I'm assuming you're already re-implementing QAbstractItemDelegate::createEditor()

The simplest way to indicate that a certain index in your table shouldn't be editable is to return NULL from this function, for example:

QWidget *QAbstractItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index )
{
  if( index.column() == 0 )
    return NULL;

  return new QSpinBox( parent );
}

You can get fancier by stuffing additional information in your model and retrieving it with QModelIndex::data()

Upvotes: 4

Related Questions