在 Tree 中查找节点。在对话框中输入要查找的节点的值

本文介绍如何在Tree数据结构中进行节点查找。通过用户在对话框中输入节点值,实现高效搜索,涉及编码处理和交互设计。
摘要由CSDN通过智能技术生成

 

<? xml version="1.0" encoding="utf-8" ?>
<!--  Searchable Tree control example. uses e4x expression to find a node with matching id attribute -->
< mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml"  creationComplete ="initApp()" >
< mx:Script > <![CDATA[
  [Bindable]
  public var _xmlData:XML;
  
  public function initApp():void
  {
    _xmlData = <element eid="hello">
         <element eid="world">
            <element eid="123"/>
            <element eid="graham"/>
            <element eid="weldon">
               <element eid="office">
                  <element eid="thing"/>
                  <element eid="boat"/>
                  <element eid="chair"/>
               </element>
               <element eid="person"/>
                </element>
             </element>
          </element> ;
   trace("test")
  }//initapp
  
  //starts at the given node, walks up the tree opening nodes as it goes
  private function expandParents(xmlNode:XML):void
  {
    while (xmlNode.parent() != null) {  
      xmlNode = xmlNode.parent();
      myTree.expandItem(xmlNode,true, false);
    }
  }//expandParents

  //uses e4x to find a node, then calls expand parents to make it visible,
  //then selects it      
private function findNodeById(sId:String):void
{
  var xmllistDescendants:XMLList  = _xmlData.descendants().(@eid == sId);
  expandParents(xmllistDescendants[0]);
  myTree.selectedItem = xmllistDescendants[0];
  }//findNodeById           
]]> </ mx:Script >

    
< mx:Panel  title ="Tree Control Example"  height ="75%"  width ="75%"  
        paddingTop
="10"  paddingLeft ="10"  paddingRight ="10"  paddingBottom ="10" >
        
< mx:HBox >
          
< mx:TextInput  id ="tiId"  text ="boat"   />
          
< mx:Button  label ="Find"  click ="findNodeById(tiId.text)"   />          
        
</ mx:HBox >
        
< mx:Tree  id ="myTree"  width ="50%"  height ="100%"  labelField ="@eid"
            showRoot
="false"  dataProvider ="{_xmlData}"   />       
    
</ mx:Panel >
</ mx:Application >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在QTreeWidget查找并返回当前节点,你可以使用QTreeWidget的findItems()方法来查找满足特定条件的项,然后使用当前项的parent()方法来获取其父节点。以下是一个示例代码: ```python from PyQt5.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem def find_current_item(tree_widget, text): # 查找匹配的项 matched_items = tree_widget.findItems(text, QtCore.Qt.MatchExactly, 0) if len(matched_items) > 0: current_item = matched_items[0] return current_item return None # 创建QTreeWidget对象 app = QApplication([]) tree_widget = QTreeWidget() tree_widget.setWindowTitle("QTreeWidget Example") # 添加根节点 root_item = QTreeWidgetItem(tree_widget) root_item.setText(0, "根节点") # 添加子节点 child_item = QTreeWidgetItem(root_item) child_item.setText(0, "子节点") # 设置子节点为当前项 tree_widget.setCurrentItem(child_item) # 查找并返回当前项 current_item = find_current_item(tree_widget, "子节点") if current_item: print("当前项文本:", current_item.text(0)) parent_item = current_item.parent() if parent_item: print("父节点文本:", parent_item.text(0)) else: print("未找到匹配项") # 运行应用程序 app.exec_() ``` 在这个示例,我们首先创建了一个QTreeWidget对象,并添加了一个根节点和一个子节点。然后,我们使用setCurrentItem()方法将子节点设置为当前项。接下来,我们调用find_current_item()函数来查找并返回当前项。如果找到了匹配项,我们打印当前项和其父节点的文本。如果没有找到匹配项,则打印"未找到匹配项"。请注意,这的find_current_item()函数是一个自定义的辅助函数,用于查找当前项。你可以根据自己的需求进行调整和扩展。 希望这可以帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值