マウスを離したときに、listView のエディターを呼び出す際に問題がありました。私は自分の問題を解決することができました。私には明らかではなかったので、解決策を投稿することにしました:
デリゲート ヘッダー ファイルでエディター ウィジェット ポインターを作成し、コンストラクターで値 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)
{
}
次にcreateEditorで:
QWidget *MyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
myCustomWidget= new KontaktForm(parent);
myCustomWidget->autoFillBackground();
return myCustomWidget;
}
MyListView ヘッダー ファイルで、シグナル saveToModelFromEditor(); を作成しました。そして信号を発しました
void MyListView::leaveEvent(QEvent *event)
{
emit saveToModelFromEditor();
QListView::leaveEvent(event);
}
commitData をモデルにコミットし、誰かがエディターを閉じたい場合にエディターを閉じる関数:
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);
}
}
最後に、シグナルとスロットのメカニズムを使用して、シグナルを listView からエディターのスロットに接続しました。
connect(treeView,SIGNAL(saveToModelFromEditor()),itemDelegate,SLOT(commitAndSaveData()));
別のコミュニティ (VoidRealms facebook グループ) から助けてもらいました。
これが誰かに役立つことを願っています。