QItemDelegate for editing questions
-
First please derive from QStyledItemDelegate, QItemDelegate will hopefully go away with Qt6.
- setEditorData()/setModelData() - you forgot QModelIndex::data() which can be used in setEditorData() as a shortcut.
- createEdtior() - it's because you can create a different editor/widget for each index
- QStyleOptionViewItem - just take a look at the parameters - it's used for drawing the item so you can use it too if you want/need it.
The stuff with the hard focus I don't understand...
-
First please derive from QStyledItemDelegate, QItemDelegate will hopefully go away with Qt6.
- setEditorData()/setModelData() - you forgot QModelIndex::data() which can be used in setEditorData() as a shortcut.
- createEdtior() - it's because you can create a different editor/widget for each index
- QStyleOptionViewItem - just take a look at the parameters - it's used for drawing the item so you can use it too if you want/need it.
The stuff with the hard focus I don't understand...
@Christian-Ehrlicher
Thanks, that's perfect. I will change my derivation.you forgot
QModelIndex::data()
which can be used insetEditorData()
as a shortcut.Funny you should say that! Earlier I was musing to myself: "If
QModelIndex
contains a reference to the model, then a convenience method to access the data would be possible...". :) That's good, but doesn't to me answer why Qt passes the model explicitly tosetModelData()
but not tosetEditorData()
. It's as though they expect you to need it in the former but not in the latter, so I wondered if I was missing something.I still wonder what that Qt hard focus thing I saw is about.... Does a widget have some "hard focus" property which affects how it receives events?
-
Maybe they mean StrongFocus?
-
Maybe they mean StrongFocus?
@Christian-Ehrlicher
You're a hero :)
Here we go https://doc.qt.io/qt-5/qabstractitemdelegate.html:The returned editor widget should have
Qt::StrongFocus
; otherwise,QMouseEvents
received by the widget will propagate to the view.What do you think of that? Do I need to take any action on the
QWidget
I am returning? -
At least I did not yet had problems with the default behavior but it maybe depends on the type of the displayed widget - with QComboBoxes I don't have a problem here.
-
At least I did not yet had problems with the default behavior but it maybe depends on the type of the displayed widget - with QComboBoxes I don't have a problem here.
@Christian-Ehrlicher
OK, mine too seems to be working without my doing anything aboutStrongFocus
so I won't worry. -
@Christian-Ehrlicher
Thanks, that's perfect. I will change my derivation.you forgot
QModelIndex::data()
which can be used insetEditorData()
as a shortcut.Funny you should say that! Earlier I was musing to myself: "If
QModelIndex
contains a reference to the model, then a convenience method to access the data would be possible...". :) That's good, but doesn't to me answer why Qt passes the model explicitly tosetModelData()
but not tosetEditorData()
. It's as though they expect you to need it in the former but not in the latter, so I wondered if I was missing something.I still wonder what that Qt hard focus thing I saw is about.... Does a widget have some "hard focus" property which affects how it receives events?
@JonB said in QItemDelegate for editing questions:
but doesn't to me answer why Qt passes the model explicitly to setModelData() but not to setEditorData()
index.model()
returns aconst QAbstractItemModel*
you can read the data from the model and set it inside the editor but you can't performs actions that alter the model from it, that's whysetModelData()
needs to have another, non-const, model passed as argument -
@JonB said in QItemDelegate for editing questions:
but doesn't to me answer why Qt passes the model explicitly to setModelData() but not to setEditorData()
index.model()
returns aconst QAbstractItemModel*
you can read the data from the model and set it inside the editor but you can't performs actions that alter the model from it, that's whysetModelData()
needs to have another, non-const, model passed as argument -
@VRonin
Ahhhhh!!!!! Well, that makes sense. I wouldn't know that, because I'm Python/PyQt where we don't have any notion ofconst
and we can change whatever we like when we like, regardless! :)@JonB said in QItemDelegate for editing questions:
Python/PyQt where we don't have any notion of const and we can change whatever we like when we like
A truly frightening thought....
-
@JonB said in QItemDelegate for editing questions:
Python/PyQt where we don't have any notion of const and we can change whatever we like when we like
A truly frightening thought....