当用户点击QTreeWidgetItem时会触发一个函数。 我想知道是否有办法以编程方式“点击”项目。我试过了
.setCurrentIndex(index)
.setCurrentItem(item)
但是当这些方法将项目设置为“活动”时,它们不会触发onClick函数。
答案 0 :(得分:1)
treeWidget.currentItemChanged
连接到onClick setCurrentItem(item)
方法
treeWidget.currentItemChanged.connect(onClick)
previousItem = treeWidget.currentItem()
treeWidget.setCurrentItem(item)
def onClick(current, previous):
print current.text(0) if current is not None else None # put the whatever index your item has
print previous.text(0) if previous is not None else None
答案 1 :(得分:1)
这里有一些代码片段来模拟鼠标点击QTreeWidget的项目。我希望它可以帮到你。
# get the QTreeWidget object
treeWidget = self.form.treeWidget
# obtain the 0th child item of the 0th top level item
child = treeWidget.topLevelItem(0).child(0)
# obtain the rectangular coordinates of the child item
rect = treeWidget.visualItemRect(child)
# simulate the mouse click within the rectangular coordinates
QTest.mouseClick(treeWidget.viewport(), Qt.LeftButton, Qt.NoModifier,rect.center())