我想用编程的方式在QTreeView中选择一行,我找到了95%的答案here。在
select()
方法完美地完成了这项工作,只是它似乎没有触发任何单击视图的事件。在
我通过自己调用所需的信号找到了一个解决方法-但是有没有任何迹象表明有一种方法可以模拟人类点击并发送所有相关的信号?在
以下是我的解决方法(在Python中):
oldIndex=treeView.selectionModel().currentIndex()
newIndex=treeView.model().indexFromItem(item)
#indexes stored----------------------------------
treeView.selectionModel().select(
newIndex,
QtGui.QItemSelectionModel.ClearAndSelect)
#selection changed-------------------------------
treeView.selectionModel().currentRowChanged.emit(
newIndex,
oldIndex)
#signal manually emitted-------------------------
因此,由于第一个信号是由select()方法发送的,所以在侦听selectionChanged()信号而不是currentRowChanged()信号时,找到了答案。在
这需要很少的修改:
相关问题 更多 >
编程相关推荐