1.自定义树形model2.TreeView实现搜索功能
TreeModel::TreeModel(QObject *parent) : QAbstractItemModel(parent){ initTreeData(); m_pRoot = new TreeItem("根节点", 0); setupModelData();}TreeModel::~TreeModel(){ delete m_pRoot;}QVariant TreeModel::data(const QModelIndex &index, int role) const{ if(!index.isValid()) { return QVariant(); } if(role == Qt::DisplayRole) { TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); return item->name; } else if(role == Qt::DecorationRole) { return QVariant(); } return QVariant();}Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const{ if(!index.isValid()) return 0; if(index.column() == 0) return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable; return Qt::ItemIsEnabled | Qt::ItemIsSelectable;}QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const{ Q_UNUSED(section) if(orientation == Qt::Horizontal && role == Qt::DisplayRole) return m_pRoot->name; return QVariant();}QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const{ if(!hasIndex(row, column, parent)) return QModelIndex(); if(!parent.isValid())//ROOT下挂装置NodeMac { TreeItem *pNodeMac = m_pRoot->getChild(row); QModelIndex modelIndex = createIndex(row, column, pNodeMac); return modelIndex; } TreeItem *pParent = static_cast<TreeItem*>(parent.internalPointer()); TreeItem *pChild = pParent->getChild(row); return createIndex(row,column,pChild); return QModelIndex();}QModelIndex TreeModel::parent(const QModelIndex &index) const{ if(!index.isValid()) { return QModelIndex(); } TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer()); TreeItem *parentItem = childItem->getParent(); if(m_pRoot == parentItem) { return QModelIndex(); } return createIndex(0, 0, parentItem); return QModelIndex();}int TreeModel::rowCount(const QModelIndex &parent) const{ TreeItem *parentItem; if(parent.column() > 0) { return 0; } if(!parent.isValid()) { return m_pRoot->getChildCount(); } parentItem = static_cast<TreeItem*>(parent.internalPointer()); return parentItem->getChildCount(); return 0;}int TreeModel::columnCount(const QModelIndex &parent) const{ Q_UNUSED(parent) return 1;}void TreeModel::setupModelData(){ QHash<QString,TreeItemData>::iterator it; for(it = m_treeDataList.begin(); it != m_treeDataList.end(); it ) { TreeItem *pParent = new TreeItem(it.key(), m_pRoot); QHash<QString,TreeItemData>::iterator it2; for(it2 = it.value().childList.begin(); it2 != it.value().childList.end(); it2 ) { TreeItem *pChild = new TreeItem(it2.key(), pParent); pParent->childList.insert(it2.key(), pChild); QHash<QString,TreeItemData>::iterator it3; for(it3 = it2.value().childList.begin(); it3 != it2.value().childList.end(); it3 ) { TreeItem *pChild3 = new TreeItem(it3.key(), pChild); pChild->childList.insert(it3.key(), pChild3); } } m_pRoot->childList.insert(it.key(), pParent); }}void TreeModel::initTreeData(){ for(int i = 0; i < 10; i ) { TreeItemData child1; child1.name = tr("一级节点%1").arg(i 1); for(int j = 0; j < 10; j ) { TreeItemData child2; child2.name = tr("二级节点%1%2").arg(i 1).arg(j 1); for(int k = 0; k < 10; k ) { TreeItemData child3; child3.name = tr("三级节点%1%2%3").arg(i 1).arg(j 1).arg(k 1); child2.childList.insert(child3.name, child3); } child1.childList.insert(child2.name, child2); } m_treeDataList.insert(child1.name, child1); }}
评论