Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What is (correctly) triggering setModelData in my Delegate?



  • I have a weird question where my qt app is working "correctly"... but I don't know why and I'd like to.

    I'm in the middle of writing a subclassed QTreeView with a QStyledItemDelegate and a custom editor widget to edit one aspect of my model in one of the columns of my view.

    The editor widget is super simple: it's just a widget with a layout and a QLineEdit and a QPushButton.

    I've implemented the model, as well as the delegate's setEditorData and setModelData methods.

    When I start the app and double click on a cell, the editor widget is created by the delegate and shows up as expected (and setEditorData is called and populates the QLineEdit with the right thing), and then when I enter in some new text inside the editor's QLineEdit and hit <Enter>, setModelData on the delegate gets called, which modifies the model as expected, and the delegate and custom widget disappear, all as expected.

    The thing is - I don't understand what is (automatically?) triggering setModelData in my delegate. I have connected nothing to the QLineEdit's editingFinished signal: my custom editor widget doesn't listen to the QLineEdit and doesn't explicitly emit any signals, and nothing else even knows about the QLineEdit's existence. I have not even written the word "commitData" anywhere in my code yet.

    My question is: what is going on when I finish editing my QLineEdit (a signal I have attached nothing to) that causes setModelData in my delegate to be called correctly? I'd like to block this automatic thing and only commitData when I want it to.

    Sorry, I can't paste my code here (workplace has copy/paste quarantine block) but the code is pretty straightforward I think.



  • QStyledItemDelegate has an event filter, QAbstractItemDelegatePrivate::editorEventFilter. When you press enter and the editor is not a QTextEdit or a QPlainTextEdit then it will invoke QAbstractItemDelegatePrivate::_q_commitDataAndCloseEditor that will call commitData



  • @VRonin Aha, that has got to be it - thank you very much! Sounds like I just need to add a new eventfilter that does nothing but eat keypress events.


Log in to reply