<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Working with focus events in item views might be a real headache, since you have to deal with the whole widget tree hierarchy a view has, including the editor.<div><br></div><div>I'd suggest another approach, which makes more sense to me: the closeEditor() is what actually closes (and eventually commits) data to the model, and it's called from key events and accepted mousePressEvent()s, which actually try to close the editor, change the current index and, eventually, create a new editor).</div><div>This is a very basic implementation.</div><div><br></div><div><div><font face="monospace, monospace">class KeepFocusTable(QtWidgets.QTableView):</font></div><div><font face="monospace, monospace">    currentEditor = None</font></div><div><br></div><div><font face="monospace, monospace">    def closeEditor(self, editor, hint):</font></div><div><font face="monospace, monospace">        self.currentEditor = editor</font></div><div><br></div></div><div><div><font face="monospace, monospace">    def mousePressEvent(self, event):</font></div><div><font face="monospace, monospace">        if self.state() == self.EditingState and</font><span style="font-family:monospace,monospace"> self.currentEditor is not None:</span></div><div><font face="monospace, monospace">            self.currentEditor.setFocus()</font></div><div><font face="monospace, monospace">            self.currentEditor.selectAll()</font></div><div><font face="monospace, monospace">        else:</font></div><div><font face="monospace, monospace">            QtWidgets.QTableView.mousePressEvent(self, event)</font></div></div><div><br></div><div>If you return closeEditor(), the editor will stay active, and the view state() will be kept as EditingState.</div><div>This allows you to keep the focus on the editor when using the [shift-]tab, enter and escape keys; I also added a currentEditor property that allows to keep track of the current editor.</div><div>The mousePressEvent() can now check the state, since it's still in EditingState and a currentEditor exists, it will focus it; the selectAll() is obviously optional, I just added so that the user can clearly see that the focus has been kept; you might also want to ensure that the cell is visible (in case the user has scrolled "out" of it) by using scrollTo().</div><div><br></div><div>At this point you can add your implementation to check and validate the contents (for example by using a QValidator inside the closeEditor implementation), and eventually call the QTableView.closeEditor original method whenever you find it appropriate.</div></div></div></div></div><div><br></div><div>I hope this helps,</div><div>Maurizio</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div></div>