[PyQt] Suppress departure from cell in QTableView

John F Sturtz john at sturtz.org
Sun Feb 17 22:19:39 GMT 2019


Greetings Maurizio.

Yes, this certainly does help!  After a little playing around, it looks like this is going to lead me to a solution.  :-)

There are still a few things I don't fully understand.

I actually did experiment with reimplementing closeEditor() awhile back.  But I reimplemented it in the delegate, not in the view.  I thought I understood closeEditor() to be a member of QAbstractItemDelegate, not QTableView.  I don't quite understand how it is possible to override this method in the QTableView definition.  (Still, empirically, it seems to work, so I must be wrong).

One slight problem with your code as written:  I found that if a delegate editor is active and I mouse click on a different cell, the mousePressEvent actually registers before the closeEditor() method is called.  You assign self.currentEditor inside closeEditor(), so on the first mouse click it isn't set yet.  However, if I assign currentEditor when the editor is created (i.e., in the delegate's createEditor() method), then it seems to work.

And with that, I find I don't seem to need to reimplement closeEditor() at all.  Just catching a mousePressEvent, checking that self.state() is EditState and calling self.currentEditor.setFocus() to set the focus back to the editor widget seems to be enough.

[Modified code that demonstrates proof of concept is attached]

I will also need to similarly catch keystrokes that try to change to focus to a different cell.  But that is easier, because those events go to the delegate, so can be handled within the delegate's eventFilter() method.

I still don't really understand why it didn't work when I tried catching and suppressing the FocusOut event.  I feel like I followed the documentation properly.  But I guess it's somehow related to the fact that there are multiple levels of widgets that the events are passing through.

In any event, it looks like I have a workaround.  Thank you!

/John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190217/6a76025d/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test.py
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190217/6a76025d/attachment.ksh>


More information about the PyQt mailing list