c++ - 在特殊情况下,如何防止来自 QFileSystemModel 的 rowsInserted 信号?
问题描述
我正在编写文件资源管理器,并且使用 QFileSysteModel 作为基础。我注意到方法QFileSystemModel::index()
和QFileSystemModel::fetchMore
导致模型发出信号rowsInserted
。
我已将rowsInserted
信号连接到一个插槽,该插槽发送有关新插入行的数据。问题是来自QFileSystemModel::index()
并且QFileSystemModel::fetchMore
不是真正新的行,而是由 QFileSystemModel 本身添加到模型中的行,这在我的程序中造成了麻烦。
我在使用之前尝试过设置标志QFileSystemModel::index()
,QFileSystemModel::fetchMore
但它并不可靠,尤其是QFileSystemModel::fetchMore
.
喜欢:
m_blockRowInsert = true; // <-- blocks rowInserted for m_fileSysModel->index
auto index = m_fileSysModel->index(pathNew); // calls immediately rowsInserted
if(index.isValid())
{
if(m_fileSysModel->canFetchMore(index))
{
m_blockRowInsert = true;// <-- does not work reliable because rowsInserted can be called more than once by fetchmore
m_fileSysModel->fetchMore(index); // <-- calls rowsInserted after completing the function
}
}
我试图像这样重置标志:
void onRowsInserted(const QModelIndex &parent, int first, int last)
{
if(m_blockRowInsert)
{
m_blockRowInsert = false;
return;
}
}
解决方案
您可以使用blockSignals功能。这样,您可以在之前阻止信号fetchMore
并在之后启用信号。
...
if(m_fileSysModel->canFetchMore(index))
{
this->blockSignals(true);
m_fileSysModel->fetchMore(index);
this->blockSignals(false);
}
...
我假设那this
是rowInserted
信号的发送者。
推荐阅读
- java - 在月底循环遍历 Joda Time 月份
- html - How do I put one container above the other in css?
- json - Chrome 正在阻止 json 下载 - 有没有办法阻止这种情况?
- angular - 渲染视图后是否可以在 DOM 元素上添加 MatTooltip?
- file-upload - 使用 Struts2 上传 CSV 文件不能正常工作
- python - 为什么我在 PyCharm 中的命令行无法识别 bash 命令?
- grafana - 不能在grafana中使用全局变量
- httprequest - 使用未签名上传云时必须指定上传预设
- python - 如何检查 2 个不同的值是否来自同一个列表并获取列表名称
- modbus - 使用 node-red 读取和转换 modbus tcp