- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从 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));
但如果我尝试只用一次发射实现相同,则 View 中所有行的所有列都会更新。
emit dataChanged(index(5, 0), index(5, 3));
我做错了什么?
演示.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();
}
最佳答案
我认为问题在于当 QAbstractItemModel::dataChanged
时您对 QTreeView
的行为所做的某些假设发出信号。
具体来说,您假设 View 只会调用 QAbstractItemModel::data
在信号中指定的那些索引上。不一定是这样。
查看 QAbstractItemView::dataChanged
的源代码(Qt
5.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
——例如topLeft
与 bottomRight
相同。如果它们 相同,则 View 会尝试确保仅更新该模型索引。但是,如果指定了多个模型索引,那么它将调用...
d->viewport->update();
这大概会导致查询所有可见模型索引的数据。
由于您实现的 Demo::data
总是根据当前时间返回新数据,您将看到 View 更新的整个可见部分,给人的印象是 dataChanged
所有行和列都发出了信号。
因此,解决方法实际上是让您的数据模型更加“有状态”——它需要跟踪值而不是简单地按需生成它们。
关于c++ - QAbstractTableModel 并为单行发出 dataChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53139727/
使用 QTableView和 QAbstractTableModel - 当模型发出 dataChanged对于正在编辑的单元格的事件,用户在单元格中键入的字符串(但未按 Enter 以“提交”编辑)
下面是我添加的行类。它由代码调用,而不是表调用,我希望它在添加新行时正确调用 dataChanged,尽管这不起作用,但表不执行任何操作。我做错了什么? void MyModel::add(const
我的问题如下: 有一个QTableView和一个QStandardItemModel是这样使用的: ui->tableView->setModel(model); model->setItem(myr
我从 QAbstractTableModel 派生了一个模型,现在我想通知一整行的数据已被更改。例如,如果更改了索引为 5 的行的数据(4 列),则使用以下代码将按预期工作。 emit dataCha
我在 QLabel 中显示 QTableWidget 中的项目计数,但每当我进行一些更新时,我都必须手动更新计数(标签)。当我删除或添加行时是否会发出任何信号?我尝试了 cellChanged 和 i
当我在这段代码中复制Google Chrome地址栏中的url时,QClipboard::dataChanged()的插槽DetectClipboardUrl有时会被调用两次,在Win7 32位上使用
我实现了一个 abstractListModel,它定期调用 dataCanged() 来更新列表。在 listmodel 中,我有一个变量,我想在 listview 之外显示,但也希望它在 list
我有一个连接到 QAbstractTableModel 子类的 QTableView。我正在编辑模型拥有的数据以响应不同控件(不是 QTableView)中的用户输入,然后我在模型上调用发出 data
我将 QML MapItemView 组件与基于 C++ QAbstractListModel 的模型一起使用。 MapItemView 在重置模型、添加新项目或删除现有项目时工作正常。但是,MapI
在 Qt 中,我有一个模型子类 QAbstractItemModel - 它是在 QTreeView 中显示的树。 该模型支持各种形式的变化,它们都可以正常工作。相关的两个是: 1)少量相关行中的一些
我正在使用 Python 和 PySide2 Qt 绑定(bind)。 我的程序旨在从 csv 文件加载记录,将它们显示为表中的行,并在询问时将每条记录上传到远程数据库。每次上传都需要几秒钟,所以
当模型以任何方式修改时,我试图施展一些魔法。对添加或删除的项目使用react非常容易,但我很难找到一种对被修改(重命名)的项目使用react的方法 model.dataChanged.connect(
大家好,这里是 Stackoverflow 中的所有 Qt 模型/ View 编程专家。我正在从事的项目要求我为 XML 数据定义一个 QAbstractItemModel 派生模型。之前在实现这个模
我希望有人可以就我观察到的 dataChanged 信号问题提供一些帮助。在使用受限的单元格范围发出此信号后,数据模型似乎在每个可见单元格上调用 data(...)。 例如: QModelIndex
例如,如果我在 QAbstractItemModel 中调整子项的大小,是否只为父索引发出一个 dataChanged 信号就足够了,或者我应该为每个 child ? 最佳答案 在我的测试中,仅仅为父
当模型数据更改时,我无法更新 QTableView 的布局。我尝试使用 dataChanged.emit(index,index)、layoutChanged.emit() 以及最后的手段 reset
下面的代码在 self.emit 行中断。它在 PyQt4 中运行良好。如何修复此代码以使其在 PyQt5 中工作? from PyQt5 import QtCore, QtGui, QtWidget
这是我在源模型中的 setData: bool TreeModel::setData(const QModelIndex& index, const QVariant& value, int role
我有一个 QTableView,它设置了一个 QStandardItemModel。用户在 View 中的某个索引中编辑数据,然后模型发出 dataChanged() 信号。在我处理 SIGNAL 的
我正在尝试制作一个 Qt 应用程序来监视对系统剪贴板的更改。每次用户将一些文本复制到剪贴板(应用程序外部)时,我的应用程序应该以某种方式更改该文本并将更改的文本复制到剪贴板。 问题是剪贴板只会变空!
我是一名优秀的程序员,十分优秀!