1

I have a tree model and use QDataWidgetMapper to map the model data to some widgets.

In the model, some of the data are flagged as read-only, so, what I would like to do is to let the mapped widget, say, a QLineEdit, be able to act upon this flag and set itself to readonly when the model data it points to is readonly.

Any help is appreciated!

Jerry Ma
  • 511
  • 4
  • 15
  • [QDataWidgetMapper.currentIndexChanged](https://doc.qt.io/qt-5/qdatawidgetmapper.html#currentIndexChanged)? – ekhumoro Feb 26 '17 at 19:10
  • Seems like an interesting feature for QDataWidgetMapper itself... mind suggesting it on the bugtracker? – peppe Feb 26 '17 at 21:33
  • @peppe I believe someone already submitted a bug report for Qt 4.8, (https://bugreports.qt.io/browse/QTBUG-8409). It was then closed due to the transition of Qt4 to Qt5. I am not familiar with how Qt bug report works, but will look into it when I have time. – Jerry Ma Feb 26 '17 at 23:05
  • @ekhumoro Thank you for the suggestion, it should work. I really hope QDataWidgetMapper could understand the flags :( – Jerry Ma Feb 26 '17 at 23:11

1 Answers1

0

Qt's QAbstractDataModel interface doesn't expose writability of a piece of data as an attribute: there's nothing you can read to know whether an item can be modified or not. One could think of some non-general hacks, such as attempting to write back the current value of the item to check if it can be changed. They are but hacks, e.g. a model fulfilling the contract mandated by Qt might return true from setData even on a read-only item if the new value equals old value.

If you're using a model that exposes the writability attribute, you'll need to derive from QDataWidgetMapper and implement that functionality yourself.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • >>Qt's QAbstractDataModel interface doesn't expose writability of a piece of data<< That's not quite true: there is `QAbstractItemModel::flags()` which should return `Qt::ItemIsEditable` for writable entries in the model and it should omit that flag for read-only entries. – Ignitor Nov 21 '18 at 12:26