6

Simple question. I'd like to use F2 or Enter for rename, and double click to open a file.

Using self.treeView.doubleClicked.connect(self.doubleclick) I can do things in my self.doubleClick method, but the renaming is still triggered.

The model is not read-only (model.setReadOnly(False)).

2 Answers 2

10

I don't know if you have this in python versions, but in C++ Qt you just set the edit triggers in the QAbstractItemView:

void setEditTriggers ( EditTriggers triggers )

2
2

By default, the doubleClicked signal is emitted just before the normal editing action, which is carried out by the QAbstractItemView.edit function.

Fortunately, this function is virtual, so it can be reimplemented in a subclass:

class TreeView(QtGui.QTreeView):    
    def edit(self, index, trigger, event):
        if trigger == QtGui.QAbstractItemView.DoubleClicked:
            print 'DoubleClick Killed!'
            return False
        return QtGui.QTreeView.edit(self, index, trigger, event)
4
  • Also note that you can press F2 to trigger renaming, so this doesn't handle that.
    – Green Cell
    Feb 20, 2017 at 3:28
  • 1
    @GreenCell. The OP specifically asked to kill double-click editing, which is what the example code does. And it can easily be adapted to handle other kinds of editing (including F2 - or more precisely, the platform edit-key).
    – ekhumoro
    Feb 20, 2017 at 4:45
  • Yes I know, but F2 is a subtle thing that can easily be overlooked. Thought I mention it in case anyone wasn't thinking about it.
    – Green Cell
    Feb 20, 2017 at 8:43
  • @GreenCell separate topic entirely, this is explicitly about DoubleClick. Easy to use trigger == QAbstractItemView.EditKeyPressed to achieve this for anyone wondering.
    – misantroop
    Dec 11, 2020 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.