简体   繁体   English

QT MVC 模式不更新视图 - 特定的 SimpleTreeModel 示例

[英]QT MVC pattern not updating view - specific SimpleTreeModel example

I have had problems in my own code to get the views to update after model data is updated.我在自己的代码中遇到了问题,无法在更新模型数据后更新视图。

I then took the SimpleTree example from QT and added a timer in TreeModel to change the data after 10s and then invoke the same set data function used in the constructor to update the model.然后我从 QT 中获取 SimpleTree 示例,并在 TreeModel 中添加一个计时器以在 10 秒后更改数据,然后调用构造函数中使用的相同设置数据函数来更新模型。 The code is below for the slot that executes on the timer timeout.下面是在计时器超时时执行的插槽的代码。 No matter what I try, the view does not update.无论我尝试什么,视图都不会更新。 The setDate and emit dataChanged were some attempts. setDate 和 emit dataChanged 是一些尝试。

In my own code, I have a XML-RPC call updating the data, but considering I dont even get the simpleTreeModel to work, I suppose that would be a long shot.在我自己的代码中,我有一个 XML-RPC 调用来更新数据,但考虑到我什至没有让 simpleTreeModel 工作,我想这将是一个漫长的过程。

Is there something fundamental that I am missing here?我在这里缺少一些基本的东西吗?

void TreeModel::slotTimeout(void)
{
   QStringList tmp;
   tmp << "qaz";
   tmp << "wsx";
   tmp << "edc";
   setupModelData(tmp,rootItem);
   setData(QModelIndex(),QModelIndex());
   emit dataChanged(QModelIndex(), QModelIndex());
   qDebug() << "Timer update";
}

The SimpleTreeModel example is for static models only. SimpleTreeModel 示例仅适用于静态模型。 It lacks the implementation of the required QAbstractItemModel functions to update the model.它缺少更新模型所需的 QAbstractItemModel 函数的实现。

Have a look on the detailed description of the models documentation in order to see what should be implemented.查看模型文档的详细描述,以了解应该实现什么。

The problem is, that the required methods are implemented as empty methods by default so you will not get any error messages if something is missing.问题是,所需的方法默认实现为空方法,因此如果缺少某些内容,您将不会收到任何错误消息。 It will just not work.它不会工作。

In addition it'sa bit tricky to do the necessary data changed emits.此外,执行必要的数据更改发出有点棘手。

Within the setData method you have to emit dataChanged() .setData方法中,您必须emit dataChanged()

Within the also necessary insertRows you have to call the methods beginInsertRows(...) and endInsertRows() in order to get the required signals emitted.在同样必要的insertRows 中,您必须调用方法beginInsertRows(...)endInsertRows()以获得发出的所需信号。

A first approach toward the MV paradigma is to use the QStandardItemModel . MV 范式的第一种方法是使用QStandardItemModel It provides all the necessary implementation if a QStandardItem is sufficient what it usually will be.如果QStandardItem就足够了,它会提供所有必要的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM