国庆节
我正在使用PySide和一些QWidgets构建一个多窗口应用程序。我有一个QTreeWidget,我想用CSV文件中的项目填充它。treeWidget有3列(静态)和动态行数。在
到目前为止,我一直在手动填充QTreeWidget,但我开始从纯粹的美学转向一个功能正常的系统。以下是我一直使用的:
items = QtGui.QTreeWidgetItem(self.treeWidgetLog)
items.setText(0, "Item 1")
items.setText(1, "Item 2")
items.setText(2, "Item 3")
这只适用于添加一行,但到目前为止已经足够了。在
我以前在Python中广泛使用csv文件,但是我不确定如何用csv中的条目填充QTreeWidget。我对此做了一些研究,但还没有找到任何具体的东西。我的基本解释是:
^{pr2}$这只是我对一个可能的解决方案的直观解释的一个快速的伪脚本。我不确定如何在向QTreeWidget添加行时合并行计数(m)。在
有什么想法吗?在
谢谢!在
编辑:以下是我正在研究的内容的快速更新:
with open('Log.csv', 'rt') as f:
reader = csv.reader(f)
m = 0
for row in reader:
n = 0
for field in row:
self.treeWidgetLog.topLevelItem(m).setText(n, field)
n = n + 1
m = m + 1
但是,上面给出了以下错误:
AttributeError: 'NoneType' object has no attribute 'setText'
我不知道为什么会这样,因为我以前见过topLevelItem().setText()。。。在
您试图在尚未创建的topLevelItem上设置文本。在
如果您只想使用csv数据填充treewidge,那么使用构造函数^{} 会更容易。在
这样,当您创建项时,它会作为topLevelItem自动添加到parentWidget中,并且您不再需要迭代csv行,因为您直接将它们传递给构造函数。在
相关问题 更多 >
编程相关推荐