Qt QSortFilterProxyModel 与自定义 TreeModel
Qt QSortFilterProxyModel with custom TreeModel
我从 Qt 中实现了这个可编辑的树模型:Editable TreeModel
一切都按预期正常工作,然后我根据我的特殊需要修改了它,我没有存储字符串,我存储了 X509 证书和它的一些一般数据,如颁发者和主题。所以我的树视图结构看起来像这样:
Subject, Issuer, Valid Till, Certificate Data
请注意,证书数据未显示在树视图中,仅显示其他三个字段。这有效,所以我的树视图可能看起来像这样,如果有由另一个证书签名的证书:
- Certificate A
- Certificate B (which was signed by A)
- Certificate C (which wassigned by B)
- Certificate D
- Certificate E
所以我实现了一个双击侦听器,其中 returns 证书数据(字段 4),工作正常。
现在我想让树视图可以排序。因此我想使用 QSortFilterProxyModel。这是正确的方法吗?或者有其他/更好的解决方案吗?
所以我尝试了以下方法并以这种方式设置了我的模型和 QSortFilterProxyModel:
QVector<X509*> certificates getCertificates(storeName);
m_model = new TreeModel(certificates);
m_proxy = new QSortFilterProxyModel;
m_proxy->setSourceModel(m_model);
ui->treeView->setModel(m_proxy);
ui->treeView->expandAll();
然后在我调用的双击侦听器中:
QVariant data = m_proxy->data(m_proxy->index(index.row(), 4));
这不正确,因为当我点击证书 B 或证书 C 时,我每次都得到存储在证书 A 下的证书数据。但我需要获取存储在正确条目下的数据 (排)。我的问题在哪里?
根据 Simons 的评论,问题的解决方案是传递父索引(树结构),因此我的代码如下所示:
QVariant data = m_proxy->data(m_proxy->index(index.row(), 4, index.parent()));
我从 Qt 中实现了这个可编辑的树模型:Editable TreeModel
一切都按预期正常工作,然后我根据我的特殊需要修改了它,我没有存储字符串,我存储了 X509 证书和它的一些一般数据,如颁发者和主题。所以我的树视图结构看起来像这样:
Subject, Issuer, Valid Till, Certificate Data
请注意,证书数据未显示在树视图中,仅显示其他三个字段。这有效,所以我的树视图可能看起来像这样,如果有由另一个证书签名的证书:
- Certificate A
- Certificate B (which was signed by A)
- Certificate C (which wassigned by B)
- Certificate D
- Certificate E
所以我实现了一个双击侦听器,其中 returns 证书数据(字段 4),工作正常。
现在我想让树视图可以排序。因此我想使用 QSortFilterProxyModel。这是正确的方法吗?或者有其他/更好的解决方案吗?
所以我尝试了以下方法并以这种方式设置了我的模型和 QSortFilterProxyModel:
QVector<X509*> certificates getCertificates(storeName);
m_model = new TreeModel(certificates);
m_proxy = new QSortFilterProxyModel;
m_proxy->setSourceModel(m_model);
ui->treeView->setModel(m_proxy);
ui->treeView->expandAll();
然后在我调用的双击侦听器中:
QVariant data = m_proxy->data(m_proxy->index(index.row(), 4));
这不正确,因为当我点击证书 B 或证书 C 时,我每次都得到存储在证书 A 下的证书数据。但我需要获取存储在正确条目下的数据 (排)。我的问题在哪里?
根据 Simons 的评论,问题的解决方案是传递父索引(树结构),因此我的代码如下所示:
QVariant data = m_proxy->data(m_proxy->index(index.row(), 4, index.parent()));