python - 向 QTreeWidget 添加一行

标签 python qt pyqt qtreewidget

如何在 PyQt 中向 QTreeWidget 添加一行? 我遇到了一些严重的麻烦 - 我有一个 QString 列表,但我不知道如何将其添加到 QTreeWidget,它位于另一个类(类 Ui_Form)中,而 QStringList 位于另一个类中。

最佳答案

首先,您必须创建一个QTreeWidgetItem列表,然后将它们添加到QTreeWidget中。 示例:

tree = your_qtreewidget  # replace every 'tree' with your QTreeWidget
strings = list_of_strings
l = []  # list of QTreeWidgetItem to add
for i in strings:
    l.append(QTreeWidgetItem(i))  # create QTreeWidgetItem's and append them
tree.addTopLevelItems(l)  # add everything to the tree

引用文献:QTreeWidgetQTreeWidgetItem在 PyQt 文档中。

关于python - 向 QTreeWidget 添加一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16770117/

相关文章:

python - 如何将文本对齐到 QTableWidget 中单元格的中心

python - PyQt5 将小部件大小设置为最小并修复

c++ - 验证数据库的结构? (C++/Qt 中的 SQLite)

c++ - 在 QTextEdit 中移动光标

python - QTreeWidget仅在按下键时拖放

python - py2exe;导入错误没有名为 colorama 的模块

c++ - QString 到 char* 的转换

python - 请求 : disable auto decoding

python - 纯密码是通过我的 python 脚本记录的

Python:从另一个文件打印字符串