1

当鼠标离开时,我在调用 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);
}

将数据提交到模型并关闭编辑器的函数,如果有人希望它关闭:

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 群组)的帮助。

希望这可以帮助这里的人。

4

1 回答 1

1

在委托头文件中,我创建了一个编辑器小部件指针,在构造函数中,我给了他值 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);
 }

将数据提交到模型并关闭编辑器的函数,如果有人希望它关闭:

 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 群组)的帮助。

希望这可以帮助这里的人。

于 2017-03-29T07:46:59.577 回答