[英]QML not registering property change from C++ property
I'm displaying data in a GridView
from a custom QAbstractListItem
subclass (implementation here ). 我在自定义
QAbstractListItem
子类( 此处实现)的GridView
显示数据。 Adding and removing items works fine, QML is notified of the changes and transitions work fine. 添加和删除项目工作正常,QML会通知更改并且转换工作正常。
Now I'm trying to set a property of an Item inside the model and have QML react on it. 现在我正在尝试在模型中设置Item的属性并让QML对其做出反应。 The problem is, the
onPropertyChanged
inside QML is not called. 问题是,没有调用QML中的
onPropertyChanged
。
Here is the property in C++: 这是C ++中的属性:
// item.h
Q_PROPERTY(bool pToBeDeleted READ toBeDeleted NOTIFY toBeDeletedChanged)
// item.cpp
void Item::requestDelete()
{
toBeDeleted_m = true;
qDebug() << "emitting";
emit toBeDeletedChanged();
}
This is what the GridView looks like: 这就是GridView的样子:
// main.qml
GridView {
id: grid
// ...
model: empty
delegate: customComponent {
toBeDeleted: pToBeDeleted
}
ListModel {
id: empty
}
}
When the program starts, grid
's model
is set to my itemmodel. 程序启动时,
grid
model
设置为我的itemmodel。
And this is the QML type that does not see the changes: 这是QML类型,看不到变化:
// customComponentForm.ui.qml
Item {
property bool toBeDeleted: false
}
// customComponent.qml
CustomComponentForm {
onToBeDeletedChanged: {
console.debug("change")
}
}
Now when I call the method from inside the model like this: 现在,当我从模型内部调用方法时,如下所示:
this->items.at(i++)->requestDelete();
The output shows emitting
but not change
. 输出显示
emitting
但不change
。
I have tried to include 我试图包括
emit dataChanged(createIndex(i, 0), createIndex(i, 0));
which did result in onToBeDeletedChanged
to be called sometimes , but that also resulted in some wonky behaviour with the error 这确实导致
onToBeDeletedChanged
有时被调用,但这也导致了一些错误的行为与错误
DelegateModel::item: index out range 3 3
Two things went wrong here. 这里出了两件事。 First, because of the ++ at
首先,因为++ at
this->items.at(i++)->requestDelete();
the dataChanged
emit had the wrong index which resulted in wrong items being updated. dataChanged
emit具有错误的索引,导致错误的项目被更新。 Second of all, 第二,
emit dataChanged(createIndex(i, 0), createIndex(i, 0));
was missing the third argument, and since in another attempt I had tried inline defining of a Vector the wrong way, I didn't find this to be the problem right away. 错过了第三个参数,并且因为在另一次尝试中我尝试以错误的方式内联定义Vector,我没有立即发现这是问题。 The right call here would have been
这里的正确电话会是
QVector<int> v;
v.append(Qt::UserRole + 7 + 1);
// pToBeDeleted being the 7th property, always check this with
// roleNames()[Qt::UserRole + i + 1]. It should say your property.
emit dataChanged(createIndex(i, 0), createIndex(i, 0), v);
My mistake. 我的错。
But on another note, since the rolename index seems to be platform dependent and signaling the change from the model is somewhat of a dirty approach, a better solution (as suggested by Kevin Krammer ) would be to rewrite the itemmodel to only contain a single property, which is the QObject
item. 但另一方面,由于角色名称索引似乎与平台有关,并且表明模型的变化在某种程度上是一种肮脏的方法,更好的解决方案(如Kevin Krammer所建议的)将重写itemmodel只包含一个属性,这是
QObject
项。 That way QML is notified of the changes item's properties have. 这样,QML 就会通知更改项目的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.