QTableView:dataChanged 事件清除正在编辑的单元格 [英] QTableView: dataChanged event clears cell being edited

查看:179
本文介绍了QTableView:dataChanged 事件清除正在编辑的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 QTableViewQAbstractTableModel - 当模型为正在编辑的单元格发出 dataChanged 事件时,用户输入的字符串单元格中的内容(但未按 Enter 以提交"编辑)被删除.

Working with a QTableView and QAbstractTableModel - when the model emits a dataChanged event for the cell being edited, the string the user has typed in the cell (but not pressed enter to 'commit' the edit) is erased.

示例:单击一个单元格,键入123",单元格仍处于编辑模式,等待更多文本,发出 dataChanged 并删除123",在编辑模式下留下一个空单元格.

Example: Click a cell, type '123', cell is still in edit mode waiting for more text, dataChanged is emitted and the '123' is erased, leaving an empty cell in edit mode.

有谁知道如何阻止这种行为,或者模型如何检测单元格何时被编辑以防止为该单元格引发 dataChanged 事件?

Does anyone know how to stop this behaviour, or how the model can detect when the cell is being edited to prevent dataChanged events being raised for that cell?

推荐答案

我遇到了同样的问题.问题是,使用不同的 role 参数调用 data() 函数.用于显示 role==Qt::DisplayRole 并在编辑时使用 role==Qt::EditRole 调用.例如尝试改变

I had the same problem. The thing is, that data() function is called with different role parameter. For displaying role==Qt::DisplayRoleand while editing it is called with role==Qt::EditRole. For example try changing

QVariant MyModel::data(const QModelIndex & index, int role) const
{
  if (role == Qt::DisplayRole)
    return QString("Text to Edit");
}

QVariant MyModel::data(const QModelIndex & index, int role) const
{
  if (role == Qt::DisplayRole || role == Qt::EditRole)
    return QString("Text to Edit");
}

应该可以解决问题

这篇关于QTableView:dataChanged 事件清除正在编辑的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆