Search code examples
c++qtlistviewmouseleaveqitemdelegate

Qt QItemDelegate Commit data and close Editor on mouse leave the view widget (listView)


I had a problem with calling off the editor of my listView, when mouse left it. I have managed to solve my problem. It was not obvious to me, so I've decided to post my solution:

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),
  myCustomWidget(Q_NULLPTR)
{
}

then in createEditor:

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

return myCustomWidget;
}

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

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

QListView::leaveEvent(event);
}

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

void MyItemDelegate::commitAndSaveData()
{
if(kontaktForm!=Q_NULLPTR){

// 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

   connect(treeView,SIGNAL(saveToModelFromEditor()),itemDelegate,SLOT(commitAndSaveData()));

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

Hope this helps someone here.


Solution

  • 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),
       myCustomWidget(Q_NULLPTR)
     {
     }
    

    then in createEditor:

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

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

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

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

     void MyItemDelegate::commitAndSaveData()
     {
     if(kontaktForm!=Q_NULLPTR){
    
     // 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

        connect(treeView,SIGNAL(saveToModelFromEditor()),itemDelegate,SLOT(commitAndSaveData()));
    

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

    Hope this helps someone here.