我需要一种基于QtreeWidgetItem
和PyQt选择name
的简单方法。
例如,我希望将名称为"five"
的项目设置为已选中。
这是我的设置的一个简单示例:
import sys
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
tree=QtGui.QTreeWidget()
itemsList = ['one']
subItemsList = ['four', 'five', 'six']
for i in itemsList:
parentItem = QtGui.QTreeWidgetItem()
tree.addTopLevelItem(parentItem)
parentItem.setText(0, i)
for z in subItemsList:
childItem = QtGui.QTreeWidgetItem(parentItem)
childItem.setText(0, z)
parentItem.setExpanded(1)
indexOfChildItem = parentItem.indexOfChild(childItem)
tree.show()
sys.exit(app.exec_())
我想在程序启动subItem
时选择名称为"five"
答案 0 :(得分:1)
这是可行的解决方案:
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QTreeWidgetItemIterator
app = QtGui.QApplication([])
tree=QtGui.QTreeWidget()
itemsList = ['one']
subItemsList = ['four', 'five', 'six']
for i in itemsList:
parentItem = QtGui.QTreeWidgetItem()
tree.addTopLevelItem(parentItem)
parentItem.setText(0, i)
for z in subItemsList:
childItem = QtGui.QTreeWidgetItem(parentItem)
childItem.setText(0, z)
parentItem.setExpanded(1)
indexOfChildItem = parentItem.indexOfChild(childItem)
iterator = QTreeWidgetItemIterator(tree, QTreeWidgetItemIterator.All)
while iterator.value():
item = iterator.value()
if item.text(0) == "five":
tree.setCurrentItem(item, 1)
iterator +=1
tree.show()
sys.exit(app.exec_())