QAbstractTableModel和发出数据单行已更改

人气:1,245 发布:2022-10-16 标签: c++ qt row onchange qabstracttablemodel

问题描述

我从QAbstractTableModel派生了一个模型,现在我想通知您,整行的数据已经更改。例如,如果索引为5的行的数据被更改(4列),则使用以下代码可以按预期工作。

emit dataChanged(index(5,0), index(5, 0));
emit dataChanged(index(5,1), index(5, 1));
emit dataChanged(index(5,2), index(5, 2));
emit dataChanged(index(5,3), index(5, 3));

但是,如果我尝试仅使用一次发出来实现相同的,则视图中所有行的所有列都将更新。

emit dataChanged(index(5, 0), index(5, 3));

我做错了什么?

最小示例(C++11、QTCreator 4.7.1、Windows 10(1803)、64位)

demo.h

#pragma once
#include <QAbstractTableModel>
#include <QTime>
#include <QTimer>

class Demo : public QAbstractTableModel
{
  Q_OBJECT
  QTimer * t;
public:
  Demo()
  {
    t = new QTimer(this);
    t->setInterval(1000);
    connect(t, SIGNAL(timeout()) , this, SLOT(timerHit()));
    t->start();
  }

  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
  {
    int c = index.column();
    if (role == Qt::DisplayRole)
    {
      QString strTime = QTime::currentTime().toString();
      if (c == 0) return "A" + strTime;
      if (c == 1) return "B" + strTime;
      if (c == 2) return "C" + strTime;
      if (c == 3) return "D" + strTime;
    }
    return QVariant();
  }

  int rowCount(const QModelIndex &) const override { return 10; }
  int columnCount(const QModelIndex &) const override { return 4; }
private slots:
  void timerHit()
  {
    //Works
    emit dataChanged(index(5,0), index(5, 0));
    emit dataChanged(index(5,1), index(5, 1));
    emit dataChanged(index(5,2), index(5, 2));
    emit dataChanged(index(5,3), index(5, 3));

    //emit dataChanged(index(5,0), index(5, 3)); // <-- Doesn't work
  }
};

main.cpp

#include "demo.h"
#include <QApplication>
#include <QTreeView>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QTreeView dataView;
  Demo dataModel{};
  dataView.setModel( &dataModel );
  dataView.show();
  return a.exec();
}

推荐答案

我认为问题出在您对QTreeView发出QAbstractItemModel::dataChanged信号时的行为所做的某些假设。

具体地说,您假设该视图将只对信号中指定的那些索引调用QAbstractItemModel::data。事实并非如此。

查看QAbstractItemView::dataChanged(Qt5.11.2)的源代码,您将看到...

void QAbstractItemView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
    Q_UNUSED(roles);
    // Single item changed
    Q_D(QAbstractItemView);
    if (topLeft == bottomRight && topLeft.isValid()) {
        const QEditorInfo &editorInfo = d->editorForIndex(topLeft);
        //we don't update the edit data if it is static
        if (!editorInfo.isStatic && editorInfo.widget) {
            QAbstractItemDelegate *delegate = d->delegateForIndex(topLeft);
            if (delegate) {
                delegate->setEditorData(editorInfo.widget.data(), topLeft);
            }
        }
        if (isVisible() && !d->delayedPendingLayout) {
            // otherwise the items will be update later anyway
            update(topLeft);
        }
    } else {
        d->updateEditorData(topLeft, bottomRight);
        if (isVisible() && !d->delayedPendingLayout)
            d->viewport->update();
    }

#ifndef QT_NO_ACCESSIBILITY
    if (QAccessible::isActive()) {
        QAccessibleTableModelChangeEvent accessibleEvent(this, QAccessibleTableModelChangeEvent::DataChanged);
        accessibleEvent.setFirstRow(topLeft.row());
        accessibleEvent.setFirstColumn(topLeft.column());
        accessibleEvent.setLastRow(bottomRight.row());
        accessibleEvent.setLastColumn(bottomRight.column());
        QAccessible::updateAccessibility(&accessibleEvent);
    }
#endif
    d->updateGeometry();
}
重要的一点是,根据信号是否指定单个QModelIndex--例如topLeftbottomRight相同,该代码的行为有所不同。如果它们相同,则该视图尝试确保只更新该模型索引。但是,如果指定了多个模型索引,则它将调用...

d->viewport->update();

这可能会导致查询所有可见模型索引的数据。

由于您的Demo::data实现总是根据当前时间返回新数据,因此您将看到视图更新的整个可见部分,从而给人一种dataChanged信号已针对所有行和列发出的印象。

因此,解决方法实际上是使您的数据模型更加"有状态"--它需要跟踪值,而不是简单地按需生成它们。

703