2016-11-15 11 views
0

プログラムでQTreeViewの行を選択すると、回答の95%が見つかりましたherePyQt:プログラムでQTreeViewの行を選択して信号を送信する

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------------------------- 
+1

あなたに関連する信号をreferrringている何の特定の? – ekhumoro

+1

@ekhumoroおそらく、選択された信号。しかし、それは実際に放出されているはずですか? – Trilarion

+0

実際、私はcurrentRowChangedシグナルが必要なので、私の場合はハックが機能しますが、人間のクリックを模倣する方法があれば疑問に思っていました。 – Gui3

答えて

1

最初はによって送信されるので、答えは、selectionChanged()信号の代わりに、currentRowChanged()にをlistenningで発見されたコメントのおかげので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) 
+0

'if isinstance(QtGui.QItemSelection、newIndex):... ' – ekhumoro

+0

はいこれはもっと正確でしょう。私は20代から0.5代になってからIFをTRYに置き換えてロードしましたが、この場合は必要ありません – Gui3

関連する問題