【问题标题】:PyQt: select row in QTreeView programmatically and emit signalsPyQt:以编程方式在 QTreeView 中选择行并发出信号
【发布时间】:2017-03-29 13:30:27
【问题描述】:

我希望以编程方式在 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-------------------------

【问题讨论】:

  • 您指的是哪些具体的相关信号?
  • @ekhumoro 可能是 selectionChanged 信号。但是,实际上应该已经发出了,不是吗?
  • 事实上,我需要 currentRowChanged 信号,所以这个 hack 在我的情况下有效,但我想知道是否有任何方法可以模仿人类点击以及由此产生的所有信号。
  • 顺便说一句,我不知道 selectionChange 是做什么的。对它感兴趣,您尝试连接它而不是 currentRowChanged,但正如文档所说,它不是信号而是插槽。你认为我可以用它来实际改变选择吗?
  • @Gui3。我认为@Trilarion 的意思是treeView.selectionModel().selectionChanged。但是,它与currentChanged 不太一样,因为它传递的是QItemSelection 对象,而不是索引。

标签: python qt pyqt selection qtreeview


【解决方案1】:

因此,感谢 cmets,在侦听 selectionChanged() 信号而不是 currentRowChanged() 时找到了答案,因为第一个是由 select() 方法发送的。

这需要很少的修改:

#in the signal connections :__________________________________________
    #someWidget.selectionModel().currentRowChanged.connect(calledMethod)
    someWidget.selectionModel().selectionChanged.connect(calledMethod)

#in the called Method_________________________________________________
#selectionChanged() sends new QItemSelection and old QItemSelection
#where currentRowChanged() sent new QModelIndex and old QModelIndex
#these few lines allow to use both signals and to not change a thing
#in the remaining lines
def calledMethod(self,newIndex,oldIndex=None):
    try: #if qItemSelection
        newIndex=newIndex.indexes()[0]
    except: #if qModelIndex
        pass
    #..... the method has not changed further

#the final version of the programmatical select Method:_______________
def selectItem(self,widget,itemOrText):
    oldIndex=widget.selectionModel().currentIndex()
    try: #an item is given--------------------------------------------
        newIndex=widget.model().indexFromItem(itemOrText)
    except: #a text is given and we are looking for the first match---
        listIndexes=widget.model().match(widget.model().index(0, 0),
                          QtCore.Qt.DisplayRole,
                          itemOrText,
                          QtCore.Qt.MatchStartsWith)
        newIndex=listIndexes[0]
    widget.selectionModel().select( #programmatical selection---------
            newIndex,
            QtGui.QItemSelectionModel.ClearAndSelect)

【讨论】:

  • if isinstance(QtGui.QItemSelection, newIndex): ...
  • 是的,这样会更准确。我是一个尝试者,因为我通过在某些加载函数中用 TRY 替换 IF 从 20 秒到 0.5 秒,但我想在这种情况下不需要它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 2023-04-03
  • 2018-07-31
  • 2016-11-13
  • 2011-07-28
  • 2011-11-19
  • 1970-01-01
相关资源
最近更新 更多