C++ 发出dataChanged时不会更新QML TreeView项
我有一个QMLC++ 发出dataChanged时不会更新QML TreeView项,c++,qt,treeview,qml,qtquickcontrols,C++,Qt,Treeview,Qml,Qtquickcontrols,我有一个QMLTreeView如下: TreeView { id: treeview model: myModel anchors.fill: parent alternatingRowColors: false headerVisible: false TableViewColumn { title: "Name" role: "name" delegate: Text {
TreeView
如下:
TreeView
{
id: treeview
model: myModel
anchors.fill: parent
alternatingRowColors: false
headerVisible: false
TableViewColumn
{
title: "Name"
role: "name"
delegate: Text
{
text: styleData.value
}
}
但是,当我从C++中的自定义方法发出数据时,什么都没有更新:
void MyModel::NameChanged()
{
// beginResetModel(); <=== Works
// endResetModel();
// emit dataChanged(QModelIndex(), QModelIndex()); // Does not work
auto topLeftIndex = createIndex(0, 0, m_root.get());
auto bottomRightIndex = createIndex(rowCount(), columnCount(), m_root.get());
(( emit dataChanged(topLeftIndex, bottomRightIndex); // Does not work.
emit dataChanged(topLeftIndex, topLeftIndex); // Does not work.
}
我的父方法:
QModelIndex parent(const QModelIndex &childIndex) const
{
if (childIndex.isValid() == false)
{
return QModelIndex();
}
auto childItem = static_cast<ModelItem*>(childIndex.internalPointer());
auto parentItem = childItem->GetParent();
if (parentItem.expired() == true)
{
return QModelIndex();
}
auto pItem = parentItem.lock();
if (pItem == nullptr)
{
return QModelIndex();
}
auto parentIndex = createIndex(pItem->GetRow(), 0, pItem.get());
return parentIndex;
}
QModelIndex父(常量QModelIndex&childIndex)常量
{
if(childIndex.isValid()==false)
{
返回QModelIndex();
}
auto-childItem=static_cast(childIndex.internalPointer());
auto parentItem=childItem->GetParent();
if(parentItem.expired()==true)
{
返回QModelIndex();
}
auto-pItem=parentItem.lock();
if(pItem==nullptr)
{
返回QModelIndex();
}
autoparentindex=createIndex(pItem->GetRow(),0,pItem.get());
返回父索引;
}
< > >强>更新:如果在C++中使用我的模型使用qTreVIEW,那么它就工作了。<>强> < p>而
emit dataChanged(createIndex(row,col),createIndex(row,col));
或者当多行/单元格更改时,第二个参数是更改的“最后一个”索引。您需要提供数据更改的有效索引。@Hayt:您确定吗?因为我读到,如果我们通过无效的索引,那么它将更新整个模型。文档中没有提到这一点:这个问题仍然是高度相关的,还没有答案!我更新了我的问题以检验你的建议。请看一看。您的
父函数()
返回什么?文档中说这些索引需要有相同的父索引。您应该有一个dummy
元素,它充当所有其他索引的父元素。但是当我修改它的一些子级时,我返回一个无效的QModelIndex(因为它们的父级是伪根)。这里解释的解决方案是:何时返回无效的QModelIndex?能否将index()和parent()函数添加到问题中?我猜你的索引有问题。我在问题中添加了索引和父方法。由于显示和重置工作正常,我实际上从未重新阅读它们,并认为它们正常。
emit dataChanged(createIndex(row,col),createIndex(row,col));