gpt4 book ai didi

c++ - Qt QItemDelegate 提交数据并关闭鼠标上的编辑器离开 View 小部件(listView)

转载 作者:行者123 更新时间:2023-11-30 05:20:53 24 4
gpt4 key购买 nike

当鼠标离开时,我无法调用 listView 的编辑器。我设法解决了我的问题。这对我来说并不明显,所以我决定发布我的解决方案:

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

希望这对这里的人有帮助。

最佳答案

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

希望这对这里的人有帮助。

关于c++ - Qt QItemDelegate 提交数据并关闭鼠标上的编辑器离开 View 小部件(listView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40461037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com