QTreeWidget 中的重复项 [英] Duplicates in QTreeWidget

查看:47
本文介绍了QTreeWidget 中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这篇文章的帮助用列表中的数据制作了一个树小部件.

I have made a tree widgety with data from a list using help from this post. https://stackoverflow.com/a/33825934/4151075. But I have a problem that it adds me duplicates to my view.

Dir1
    Dir2
        A.txt
    Dir2
        B.txt

I have managed to fix this for top level folders, but I have a problem with inner ones.
I have difficulties with accessing childs od childs in items variable.
Isn't there any parser included in Qt for viewing list of paths?
Best regards

解决方案

I have solved my problem and created a method that takes a list of paths as an argument and return a list of QWidgetItems.

def tree_widget_list(self, show_list):
    """
    Creates a list for updating tree widget
    :param show_list:
    :return:
    """
    items = []
    for item in show_list:
        item_parts = item.split('\\')

        entry = QtGui.QTreeWidgetItem(None, [item_parts[0]])
        items_text = [i.text(0) for i in items]
        if entry.text(0) not in items_text:
            parent_item = entry
        else:
            parent_index = items_text.index(entry.text(0))
            parent_item = items[parent_index]

        if len(item_parts) > 1:
            for i in item_parts[1:]:
                child_item = QtGui.QTreeWidgetItem(None, [i])
                child_list_text = [parent_item.child(i).text(0) for i in xrange(parent_item.childCount())]
                if child_item.text(0) in child_list_text:
                    child_index = child_list_text.index(child_item.text(0))
                    parent_item = parent_item.child(child_index)
                else:
                    parent_item.addChild(child_item)
                    parent_item = child_item
        items.append(entry) if entry.text(0) not in items_text else None
    return items

Then just use:

show_list = ["ABC\\AAA.txt", "DEF\\abc.txt", "ABC\\readme.txt"]
items = self.tree_widget_list(show_list)
self.cfg_src_tree.insertTopLevelItems(0, items)

For example for list ['MODULE\\designer\\qaxwidget.dll', 'MODULE\\designer\\qquickwidget.dll', 'MODULE\\designer\\qwebview.dll', 'MODULE\\iconengines\\qsvgicon.dll', 'MODULE\\imageformats\\qdds.dll', 'MODULE\\imageformats\\qgif.dll', 'MODULE\\imageformats\\qicns.dll', 'MODULE\\imageformats\\qico.dll', 'MODULE\\imageformats\\qjp2.dll', 'MODULE\\imageformats\\qjpeg.dll', 'MODULE\\imageformats\\qmng.dll', 'MODULE\\imageformats\\qsvg.dll', 'MODULE\\imageformats\\qtga.dll', 'MODULE\\imageformats\\qtiff.dll', 'MODULE\\imageformats\\qwbmp.dll', 'MODULE\\imageformats\\qwebp.dll', 'MODULE\\platforms\\qminimal.dll', 'MODULE\\platforms\\qoffscreen.dll', 'MODULE\\platforms\\qwindows.dll', 'MODULE\\printsupport\\windowsprintersupport.dll', 'MODULE\\sqldrivers\\qsqlite.dll', 'MODULE\\sqldrivers\\qsqlmysql.dll', 'MODULE\\sqldrivers\\qsqlodbc.dll', 'MODULE\\sqldrivers\\qsqlpsql.dll'] We have got:

这篇关于QTreeWidget 中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆