1

ツリーの選択を特定の列に制限しようとしています。

私はデリゲートを多用して、カスタムのアイテムごと、列ごとの動作、エディターなどを作成しています。イベントなどをブロックすることで、デリゲートからこれを行うことができると期待していました。問題は、拡張選択を模倣する完全にカスタムのソリューションを作成する必要があると思います。

ただし、多くの検索とごくわずかな例の後で、ツリービューにカスタムQItemSelectionModelが必要なようです。この仮定は正しいですか?

拡張選択モードを使用するが、特定の列にない場合は選択を無視または元に戻すことができるカスタムQItemSelectionModelを作成するにはどうすればよいですか。つまり、別の列をクリックしても、選択が変更されないようにする必要があります(選択または選択解除しないでください)。

選択モデルを入手したら、それを追加する方法を知っています。派生クラスを実装するための支援を求めています(これが接続された信号で実行できる場合を除く)。

私はPythonを使用していますが、どんな助けでも価値があります。

ありがとうございました、

[編集:]私はこれらの同様の質問を見つけました:http: //lists.qt.nokia.com/pipermail/qt-interest/2010-September/027647.html

QItemSelectionModelをサブクラス化し、両方のselectメソッドを再実装して、必要な動作を実現します。列が0より大きい範囲の部分を無視します。...または、flags()を再実装して、アイテムを選択できないようにします。それかどうかはわかりません。副作用があります。」

QTreeWidgetItemにフラグを再実装しようとしましたが、呼び出されませんでした。

def flags(self, index):
    print index.column()
    return super(DDOutlinerBaseItem, self).flags(index)
4

2 に答える 2

4

理論的には、次の調整が機能するはずです。

上記のソリューションでは、2つの別々のメソッドと@pyqtSlotデコレータを使用して、オーバーロードされたメソッド名を明確にすることができます。

@pyqtSlot(QModelIndex, QItemSelectionModel.SelectionFlags)
  def select(self, index, command):
    # ...

@pyqtSlot(QItemSelection, QItemSelectionModel.SelectionFlags)
  def select(self, selection, command):
    #...

これにより、メソッドの実装で特定のクラスのインスタンスをチェックする必要がなくなります。

于 2012-11-30T15:09:40.187 に答える
0

最初の興味深い点は、Pythonはメソッドをオーバーロードできないため、私のselectメソッドは引数0のタイプごとに1回ずつ、単純に2回呼び出されるように見えることです。これと基本的な設定を説明する例を次に示します。私のQTreeWidgetのツリーは「ツリー」と呼ばれていました(self.tree

    # in __init__ of my QTreeWidget:
    sel_model = ColumnSelectionModel(self.tree.model())
    self.tree.setSelectionModel(sel_model)

class ColumnSelectionModel(QtGui.QItemSelectionModel):
    def select(self, selection, selectionFlags):
    """
    Runs both QItemSelectionModel.select methods::

        1. select(QtCore.QModelIndex, QItemSelectionModel.SelectionFlags)
        2. select(QtGui.QItemSelection, QItemSelectionModel.SelectionFlags)

    The first seems to run on mouse down and mouse up.
    The second seems to run on mouse down, up and drag
    """
    print("select(%s,  %s)" % (type(selection), type(selectionFlags)))

    if isinstance(selection, QtGui.QItemSelection):
        infos = []
        for index in selection.indexes():
            infos.append(("index=%s row=%s column=%s" 
                                    % (index, index.row(), index.column())))

        print ", ".join(infos)
    elif isinstance(selection, QtCore.QModelIndex):
        index = selection
        print("index=%s row=%s column=%s" % (index, index.row(), index.column()))
    else:
        raise Exception("Unexpected type for arg 0: '%s'" % type(selection))

    super(ColumnSelectionModel, self).select(selection, selectionFlags)

これは私の問題を解決するようです:

class ColumnSelectionModel(QtGui.QItemSelectionModel):
    def __init__(self, model):
        super(ColumnSelectionModel, self).__init__(model)

        self.selectable_columns = [0]
        """ Set the columns that are allowed to be selected """


    def select(self, selection, selectionFlags):
        """
        Ignores any selection changes if an item is not in one of the columns
        in the self.selectable_columns list.

        Is run by both QItemSelectionModel.select methods::

            1. select(QtCore.QModelIndex, QItemSelectionModel.SelectionFlags)
            2. select(QtGui.QItemSelection, QItemSelectionModel.SelectionFlags)

        The first seems to run on mouse down and mouse up.
        The second seems to run on mouse down, up and drag
        """
        if isinstance(selection, QtGui.QItemSelection):
            # This is the overload with the QItemSelection passed to arg 0
            # Loop over all the items and if any are not in selectable_columns
            #   ignore this event. This works because it is run for every change
            #   so the offending selection index will always be the newest
            indexes = selection.indexes()
            for i in xrange(len(indexes)):
                index = indexes[i]
                if not index.column() in self.selectable_columns:
                    return
        elif isinstance(selection, QtCore.QModelIndex):
            # This is the overload with the QModelIndex passed to arg 0
            # If this index isn't in selectable_columns, just ignore this event
            index = selection
            if not index.column() in self.selectable_columns:
                return
        else:  # Just in case
            raise Exception("Unexpected type for arg 0: '%s'" % type(selection))

        # Fall through. Select as normal
        super(ColumnSelectionModel, self).select(selection, selectionFlags)

最終的な実装では、決定を委任システムに委任することを計画しています。これにより、これが一般的になり、理論的には、必要なインデックスを動的に無視できるようになります。

于 2012-06-06T18:50:20.883 に答える