Get key modifiers when editting QAbstractItemModel
-
Hello,
I have subclassed a QAbstractItemModel to feed a QTableWidget. Re-implementing setData() works well. Now, I would like to be able to check if CTRL key was pressed while entering in setData() function so that I could have different behaviors if modifier key was pressed or not. Unfortunately, I have no idea about how to get a keyevent (or key modifier) check in the QAbstractItemModel. Could someone help me ?
Thanks in advance ! -
Hi and welcome to devnet,
A model is not the right place for that, they shouldn't have to know anything about key events.
What exactly are you trying to achieve ?
In any case, you should take a look at QStyledItemDelegate.
-
Why not simply use QGuiApplication::keyboardModifiers()?
-
QGuiApplication::keyboardModifiers()? is a good idea.
I have just used it in my setData function of QAbstractItem model and it works see hereunder picture: -
Thanks for all your replies.
SGaist - my use case is the following one : columns are index of a list of objects in my model and lines are different properties of these objects. When modifying a cell with CTRL key pressed, I would like to modify the same propery (corresponding to the editted line number) of all objects with the same value. If not pressed, I only modify the property of the object with current column index. I hope my explanation is clear enough. I am interested if there is a cleaner way to get this kind of mechanism.
Christian Ehrlicher/Slawomir_Piernikowski - It seems to answer my issue, I did not know well QGuiApplication class. Thanks for your answer, I will try it tomorrow !
-
I'd rather implement that at the view level. Otherwise you might have strange behaviours if your model is used through another view or if
setData
is called from somewhere else than your current view. -
What kind of editor are you using ?
-
Then one possibility would be to implement a custom QStyledItemDelegate and reimplement setModelData.