質問

QTreeViewで特定の支部のすべての子供を拡大または崩壊させることができるようになりたいです。 Pyqt4を使用しています。

Qtreeviewには拡張されたすべての子供の機能があります *には *2つのことが必要です。別の重要な組み合わせ(シフトスペース)にバインドする必要があります。また、すべての子供も崩壊させる必要があります。 。

これまでに試したことがあります。QTreeViewのサブクラスがあり、シフトスペースのキーコンボをチェックしています。 QModelIndexが「子供」機能を持つ特定の子供を選ぶことができることを知っていますが、それは子供の数を知る必要があります。私 午前 内部ポインターを見ることで子供の数を得ることができますが、それは私に階層の最初のレベルの情報を与えてくれるだけです。再帰を使用しようとすると、たくさんの子供数を得ることができますが、これらを有効なqmodelindexに戻す方法について迷子になります。

ここにいくつかのコードがあります:

def keyPressEvent(self, event):
    """
    Capture key press events to handle:
    - enable/disable
    """
    #shift - space means toggle expanded/collapsed for all children
    if (event.key() == QtCore.Qt.Key_Space and 
        event.modifiers() & QtCore.Qt.ShiftModifier):
        expanded = self.isExpanded(self.selectedIndexes()[0])
        for cellIndex in self.selectedIndexes():
            if cellIndex.column() == 0: #only need to call it once per row
                #I can get the actual object represented here
                item = cellIndex.internalPointer()
                #and I can get the number of children from that
                numChildren = item.get_child_count()
                #but now what? How do I convert this number into valid
                #QModelIndex objects? I know I could use: 
                #   cellIndex.child(row, 0)
                #to get the immediate children's QModelIndex's, but how
                #would I deal with grandchildren, great grandchildren, etc...
                self.setExpanded(cellIndex, not(expanded))
        return

これが私が調査していた再帰方法の始まりですが、実際に拡張状態を設定しようとしているときに立ち往生します。

def toggle_expanded(self, item, expand):
    """
    Toggles the children of item (recursively)
    """
    for row in range(0,item.get_child_count()):
        newItem = item.get_child_at_row(row)
        self.toggle_expanded(newItem, expand)
    #well... I'm stuck here because I'd like to toggle the expanded
    #setting of the "current" item, but I don't know how to convert
    #my pointer to the object represented in the tree view back into
    #a valid QModelIndex
    #self.setExpanded(?????, expand)   #<- What I'd like to run
    print "Setting", item.get_name(), "to", str(expand) #<- simple debug statement that indicates that the concept is valid

これを見るために時間を割いてくれてありがとう!

役に立ちましたか?

解決

わかりました...兄弟は実際に私が行きたかった場所に私を連れて行きませんでした。コードを次のように動作させることができました(そして、それはまともな実装のようです)。兄弟にまだ称賛されています。

コードには次の仮定があることに注意してください。基礎となるデータストアオブジェクトには、子供が何人いるかを報告できることを前提としています(私のコードにget_child_count())。そうでない場合、あなたはどういうわけか子供の数を異なって取得する必要があります...おそらく、qmodelindex.child(列、col)を使用して、子どものインデックスを任意にしようとするだけで、戻るまで増え続ける行数が増えます。無効なインデックス? - これは、生理教授が提案したことであり、私はまだそれを試してみるかもしれません(それは、私のデータストアからそれを要求することで子供をカウントする簡単な方法をすでに持っているだけです)。

また、拡張しているか崩壊しているかに基づいて、再帰の異なるポイントで各ノードを実際に拡張/コルパーゼすることに注意してください。これは、試行錯誤を通じて、コードの1か所でそれをしただけで、アニメーションのツリービューがutt音とポップがポップすることを発見したためです。さて、私がトップレベルにいるかどうかに基づいて私がそれを行う順序を逆にすることにより(つまり、私が影響を与えているブランチのルート - ツリービュー全体のルートではなく)、私は素敵な滑らかなアニメーションを取得します。これを以下に文書化します。

次のコードは、qtreeviewサブクラスにあります。

#---------------------------------------------------------------------------
def keyPressEvent(self, event):

    if (event.key() == QtCore.Qt.Key_Space and self.currentIndex().column() == 0):
        shift = event.modifiers() & QtCore.Qt.ShiftModifier
        if shift:
            self.expand_all(self.currentIndex())
        else:                
            expand = not(self.isExpanded(self.currentIndex()))
            self.setExpanded(self.currentIndex(), expand)


#---------------------------------------------------------------------------
def expand_all(self, index):
    """
    Expands/collapses all the children and grandchildren etc. of index.
    """
    expand = not(self.isExpanded(index))
    if not expand: #if collapsing, do that first (wonky animation otherwise)
        self.setExpanded(index, expand)    
    childCount = index.internalPointer().get_child_count()
    self.recursive_expand(index, childCount, expand)
    if expand: #if expanding, do that last (wonky animation otherwise)
        self.setExpanded(index, expand)


#---------------------------------------------------------------------------
def recursive_expand(self, index, childCount, expand):
    """
    Recursively expands/collpases all the children of index.
    """
    for childNo in range(0, childCount):
        childIndex = index.child(childNo, 0)
        if expand: #if expanding, do that first (wonky animation otherwise)
            self.setExpanded(childIndex, expand)
        subChildCount = childIndex.internalPointer().get_child_count()
        if subChildCount > 0:
            self.recursive_expand(childIndex, subChildCount, expand)
        if not expand: #if collapsing, do it last (wonky animation otherwise)
            self.setExpanded(childIndex, expand)

他のヒント

model.rowcount(index)は、必要な方法です。

model = index.model()   # or some other way of getting it
for i in xrange(model.rowCount(index)):
  child = model.index(i,0, index)
  # do something with child

model.index(row、col、parent)は、index.child(row、col)の呼び出しと本質的に同じです。間接的なものが少ない。

QTreeViewを継承するQTreeWidgetを使用することをお勧めします。その後、子供たちをqtreewidgetitemとしてつかむことができます。

QTreeWidgetを使用したくないが、現在のモデルに固執したいので.. .isValid()を使用して「可能な」子供を反復させることができます。ただし、internalPointer()を使用しないでください。代わりに、元のModalIndexであるため、持っているセロリテムを使用します。その後、兄弟を見つけようとします。何かのようなもの

x = 0; y =0
while cellIndex.sibling(x, y).isValid():
    child = cellIndex.sibling(x, y)
    x += 1
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top