[PyQt] Suppress departure from cell in QTableView

John F Sturtz john at sturtz.org
Sat Feb 16 18:52:12 GMT 2019


On 2/16/2019 8:34:21 AM, Colin McPhail <colin.mcphail at mac.com> wrote:
Hi John,


I added a simple reimplementation of focusNextPrevChild() to the EditWidget class in your example:

    def focusNextPrevChild(self, next):
        print(f'focusNextPrevChild({next})')
        text = self.text()
        if len(text) == 3:
            return False
        return True

This causes TAB and Shift-TAB to be ignored unless the line editor contains exactly three characters. It doesn't prevent focus moving from a partially edited cell by means of cursor movement and mouse click, however.

Regards,
Colin

Hi Colin.  Thanks for the response!

This is interesting, insofar as I was unaware of focusNextPrevChild(), so it's new information to me.

However, I already had been able to produce that effect by catching the Tab and shift-Tab keypress events (as well as the other keypress events that depart the editor cell) in the EditWidget event handler.  That worked fine.

Then I realized you can depart the cell by mouse movement and mouse click as well.  So I set about the task of trying to catch mouse click events and suppress them also.  That I couldn't get to work.  It was several days ago, and I can't remember the details.  But I think the problem was this:  If you click outside the edit cell, it isn't the editor widget that receives the mouse click event (which is what you'd expect, I guess).  I think it's the QTableView object.  By the time the View detects the mouse click, focus has been lost already from the edit widget, and it's too late to prevent it.  [I'd have to go back and look at it again, but my recollection is that no matter how I tried to intercept mouse clicks, the editor widget always lost focus anyhow].

That was what put me onto the path of trying to catch and suppress the FocusOut event.  I'm keenly curious as to why what I did doesn't work.  It seems from all I've read that it should.

I see there is also a focusOutEvent() method that can be reimplemented.  But alas, it doesn't appear to allow any way to specify that the event should be suppressed.  I'm starting to wonder if there just isn't any way to do this.  That would surprise me ...

Thanks again.

/John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190216/d0ee6635/attachment.html>


More information about the PyQt mailing list