简体   繁体   English

遍历 QTreeWidget 节点

[英]Iterating through QTreeWidget Nodes

As I posted several times over the past few months, I'm writing a journal/diary application in Qt.正如我在过去几个月中多次发布的那样,我正在 Qt 中编写日记/日记应用程序。 Entries are sorted in a QTreeWidget by year, month, day, and entry (default config that sorts entries by day) or by year, month, and entry (where all entries from the same month are grouped together)条目在 QTreeWidget 中按年、月、日和条目(按天对条目排序的默认配置)或按年、月和条目(其中来自同一个月的所有条目分组在一起)进行排序

The entry nodes have two columns: The first one is visible and holds the entry name.入口节点有两列:第一列是可见的并保存入口名称。 The second column is invisible and holds the row number of the corresponding entry in the database.第二列是不可见的,它保存数据库中相应条目的行号。 When that entry is selected, the program does a select query based on that row number and displays the content.选择该条目后,程序会根据该行号执行 select 查询并显示内容。 Root, year, month, (and day, if enabled) nodes also have a second column but the row number on those is always -1.根、年、月(和日,如果启用)节点也有第二列,但其上的行号始终为 -1。 (valid row count starts at 0) (有效行数从 0 开始)

The journal toolbar already has back and forward buttons that lets the user view the next and previous entries.日记工具栏已经有后退和前进按钮,可以让用户查看下一个和上一个条目。 While this function already works, the current selected item in the tree doesn't change with it, and that's what I'm trying to fix.虽然这个 function 已经可以工作,但树中当前选定的项目不会随之改变,这就是我要修复的问题。

I've decided that the best way to do this is a loop function that scans the second hidden column values of each until the correct row number is found.我决定最好的方法是循环 function 扫描每个隐藏列的第二个值,直到找到正确的行号。 Each click of the back/forward buttons will call this function again so the selected node will always match the current entry being viewed once I get this working.每次单击后退/前进按钮都会再次调用此 function,因此一旦我开始工作,所选节点将始终与正在查看的当前条目匹配。

The drawback is that this method can be slow if the database gets huge, but there's not much I can do about that.缺点是如果数据库变得很大,这种方法可能会很慢,但我对此无能为力。 The user may delete entries or shuffle them around, so simply relying on rownumber++ or rownumber-- could cause problems.用户可能会删除条目或将它们随机播放,因此仅依赖 rownumber++ 或 rownumber-- 可能会导致问题。 Since the database doesn't fill in missing row numbers but just goes on to the next one, there could be problems if the program always assumes that every row ever made in the database still exists at any given time.由于数据库不会填写缺失的行号,而只是继续下一个行号,因此如果程序始终假定数据库中的每一行在任何给定时间仍然存在,则可能会出现问题。

My question is how do I scan a particular column of every node in a QTreeWidget?我的问题是如何扫描 QTreeWidget 中每个节点的特定列?

Iterating through all items can be done with: 迭代所有项目可以通过以下方式完成:

QTreeWidgetItemIterator it(treewidget);
while (*it) {
  if ((*it)->text(column_number)=="searched")
    break;
  ++it;
}

but maybe QTreeWiget::findItems() is just what you need. 但也许QTreeWiget :: findItems()正是你需要的。

Also look at QStandardItem::data(), it's a better way of storing per-item hidden data, compared to a hidden column. 另外看一下QStandardItem :: data(),与隐藏列相比,它是存储每项隐藏数据的更好方法。

def iterator_treewiget(self, widget:QTreeWidgetItem, content:str):
    for i in range(widget.childCount()):
        item  = widget.child(i)
        text = item.text(0)
        
        if content in text:
            self.treeWidget.setCurrentItem(item)
            return
        self.iterator_treewiget(item, content)

def search_in_tree(self):
    content = self.lineEdit_2.text().strip()
    if content:
        root = self.treeWidget.invisibleRootItem()
        self.iterator_treewiget(root, content)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM