5

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)).

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
iacopo
  • 663
  • 1
  • 7
  • 22

2 Answers2

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 )

gremwell
  • 1,419
  • 17
  • 23
  • Just revisited this, and you're right: I completely overlooked `setEditTriggers`. – ekhumoro Nov 25 '15 at 23:30
  • 1
    For examples of how to use this, see http://stackoverflow.com/questions/18831242/qt-start-editing-of-cell-after-one-click/31197990#31197990 – Donald Duck Sep 28 '16 at 13:44
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)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Also note that you can press F2 to trigger renaming, so this doesn't handle that. – Green Cell Feb 20 '17 at 03: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](https://doc.qt.io/qt-4.8/qabstractitemview.html#EditTrigger-enum)). – ekhumoro Feb 20 '17 at 04: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 '17 at 08: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 '20 at 00:43