Important: Please read the Qt Code of Conduct -

Calling of editor of custom delegate, when mouse leaves treeView (listView)

  • Hello,
    I have a problem with calling off the editor of my itemDelegate on mouse leaving the listView widget.
    I've managed to implement a custom widget as an editor for a custom treeView, by sub-classing the QStyledItemDelegate.
    Also, when mouse enters my sub-classed treeView (or listView) i call the editor (mousetracking(true) and re-implementation of mouseMoveEvent).
    But I still have a problem with calling off the editor, when mouse leaves the treeView . It is crucial, because only when the editor is called of, the data are saved to the model.
    I have found this topic:
    but don't understand just where should I use IndexAt(), i mean this post:

      I have subclased QTableView, activated mouse tracking and reimplemented the    
      mouseMoveEvent. Then using indexAt I can get the model index under the mouse 
      cursor. If the mouse is hover a selected row and hover the my rating column I call edit.
      In my editor I have reimplemented the leaveEvent for closing the editor when the mouse 
      cursor leaves it. I am using the mouseReleaseEvent in my editor for commiting data to   
      my model. 

    I know, that there is another option: I can subclass the leave event of the widget (i can catch the event of mouse leaving the widget) , but I do not know what to do next (how to call of the editor).
    Does anyone know of any examples for handling mouse leave event (or how to call mouse release on mouse leaving the treeView widget)?

  • I have managed to solve my problem.

    In delegate header file Ive created a editor widget pointer, and in constructor, I gave him the value Q_NULLPTR.

    //in header file of Delegate
    mutable QWidget *myCustomWidget;
    //in the source file of Delegate
    MyItemDelegate::MyItemDelegate(QObject *parent) : QStyledItemDelegate(parent),

    then in createEditor:

    QWidget *MyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
        myCustomWidget= new KontaktForm(parent);
        return myCustomWidget;

    in MyListView header file I've created a signal saveToModelFromEditor(); and emited the signal in

    void MyListView::leaveEvent(QEvent *event)
        emit saveToModelFromEditor();

    The function to commitData to the model and close the editor, if someone wants it to close:

    void MyItemDelegate::commitAndSaveData()
    // after testing the UI I've decided, that the editor should remain open, and just commit data
        emit commitData(kontaktForm);
    //    emit closeEditor(kontaktForm);

    Finally I've used signal and slot mechanism to connect signal from listView to slot in the editor


    I had help from a different comunity (VoidRealms facebook group).

    Hope this helps someone here.

Log in to reply