我有一个 QTreeView 设置显示 QSortFilteredProxyModel 在 filterAcceptsRow() 中自定义过滤,只接受取决于父行值的值的行(通过 QStyledItemDelegate 派生类在 QComboBox 中选择。例如,如果我在一行中选择“类型” 值“宝马”,我想为这个特殊情况显示孩子。这对第一个项目很有用,我可以在 Item1 中选择这些值多个级别(最多 5 个级别)。一切都很好。
但是,我注意到,一旦我使用 item1 和此自定义过滤,然后添加 item2,问题就开始了。我注意到的第一件事是,QTreeView 确实取消了 item1/item2 的公共父项,并且只显示了根项的子项。正常行为(以及添加 item1 的行为确实如此)是扩展 item1 的父项。为什么这会对第二个项目感到困惑,并且只有当我一直在玩 item1 的儿童设置 idk 时。此外,我注意到在 item2 中过滤无法正常工作。我在 item2->Type 中有一个 QComboBox,其子项取决于 item2->Type 值。. QComboBox 的默认值的第一个子项仍会按应有的方式显示,但是在更改 QComboBox 时,视图不再更新,并且留在 item2/item3/... 中的第一个加载的子项 但是,使用正确的值/索引调用 Delegate::setModelData 并相应地调用 TreeItem->setData() 。只是过滤似乎卡在了 TreeItem->data() 的旧值上。请再次注意,这只发生在我玩过 item1 之后。如果我不碰那个项目,我可以正确玩所有其他项目(item2,item3,...)并且它做得很好。
如果有人可以提供帮助,我将非常高兴。
有没有人给我提示,什么可能导致扩展状态的“重置”,即使 expand() 被称为 item1 和 item2 插入并且对 item1 很好用?什么可能导致我的进一步问题?
ExampleDelegate.h
类 ExampleDelegate :
公共 QStyledItemDelegate
{
Q_OBJECT
上市:
ExampleDelegate(QObject* parent = 0);
~ExampleDelegate(void);
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void setEditorData(QWidget* editor, const QModelIndex &index) const;
}
代理模型.h
类代理模型:
公共 QSortFilterProxyModel
{
Q_OBJECT
上市:
ProxyModel(QObject *parent);
〜代理模型(无效);
无效刷新();
无效doReset();
pthread_mutex_t* proxyMutex;
int rowCount(QModelIndex& parent) const;
bool hasChildren ( const QModelIndex & parent = QModelIndex() ) const;
QModelIndex parent(const QModelIndex &index) const;
QModelIndex index(int row, int col, QModelIndex& parent) const;
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
};
代理模型.cpp
bool ProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
printf("ProxyModel::filterAcceptRow \n");
pthread_mutex_lock(this->proxyMutex);
QAbstractItemModel* source = this->sourceModel();
QModelIndex index = source->index(sourceRow, 0, sourceParent);
TreeItem* item = ((TreeItem*) index.internalPointer());
如果(项目!= NULL){
int showOnly = item->showOnlyParentDetailDialog;
如果(仅显示!= -1){
TreeItem* parent = ((TreeItem*) index.internalPointer())->parent();
如果(父!= NULL){
int parentOption = parent->selectedIndex;
//printf("parentOption: %u \n", parentOption);
bool result = (showOnly == parent->selectOptions->at(parentOption).detailDialog);
pthread_mutex_unlock(this->proxyMutex);
返回结果;
} 别的 {
pthread_mutex_unlock(this->proxyMutex);
返回真;
}
}
}
pthread_mutex_unlock(this->proxyMutex);
返回真;
}