Qt World Summit: Register Today!

setEditorData not being called in Custom Delegate

  • I have been working on implementing a custom delegate for a tree view with a standard item model for the past few days and am a little stuck. I have one problem and one question.

    setEditorData Problem:

    The problem I am having is setEditorData is never being called so my editors are not being populated with the correct data. The strange part is that my other functions createEditor, setModalData and updateGeometry are all being called correctly. For the life of me I can't come up with a reason as to why the program would skip this function entirely in my delegate. Any suggestions would be really helpful. The work around that I have in place is just setting the data when the editor is created, but this seems silly when there is a setEditorData function.

    QDialog w/ Custom Delegate Question:

    The second part of my post has to do with a question regarding a QDialog as an editor in my delegate. I think I have everything working but I want to make sure I am going about it the right way. For certain types of data stored in my model I need to edit them using a custom dialog that I created. The dialog pops up no problems, the use can edit but here is where my question comes in. I have two buttons that either accept the result using accept() or reject the changes using reject(). There is also a dialog function that gets the currently entered data and returns it as a list. My setModelData function calls this dialog function and then takes the returned result and sets it to the model. Does this sound correct or is there a better way I should go about it? Does the delegate handle closing the editor even though I never call close() on the button press or in the delegate functions?


    • Why isn't setEditorData being called?
    • What is the proper way to return data from a QDialog and save it to the underlying model in a delegate?

  • So it turns out I ended up solving my own problem. The issue I was having was in my setEditorData definition I had written:
    setEditorData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)
    instead of:
    setEditorData(QWidget *editor, const QModelIndex &index)
    because the virtual declaration of setEditorData was different from my definition of inputs the method was never defined and therefore was never implemented. All in all kind of dumb mistake on my part but that was the solution.

    I am still curious about my second question though, if I am approaching things the right way with the dialog editor.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The logic is the following:

    1. You create your widget in createEditor
    2. You get the data from that widget in setModelData, it's the first parameter of the function. You just have to cast it to the right type using qobject_cast

    Hope it helps