[PyQt] Suppress departure from cell in QTableView

John F Sturtz john at sturtz.org
Fri Feb 15 03:52:14 GMT 2019


Hi.  [Windows 10/Python 3.7.1/PyQt 5.11.3; My first time posting here, so please pardon in advance if anything is not quite right ...]

I have a QTableView with associated QAbstractTableModel.  The View uses a Delegate that creates a QLineEdit widget for cell editing.

I think (I hope) what I'm trying to do is conceptually very simple:  When editing in a cell is in progress, I want to prevent departure from the cell.  (This would typically be under circumstances where the cell contents are determined not to be valid).  For example, while editing the contents of a cell, the user hits Tab to move to the next cell.  I want to suppress that and leave the cursor right where it is, as though nothing happened.

After reading the QEvent documentation, I think I should be able to do this by filtering and suppressing either the FocusAboutToChange or FocusOut events.

So I define a QObject to serve as the event filter, and gave it an eventFilter() method.  Then I use installEventFilter() to install it as the event filter for the QLineEdit.

If I start editing in a cell, then hit (for example) the Tab key, the eventFilter() method properly detects the FocusAboutToChange and FocusOut events.  eventFilter() returns True for both of these, which should (I think) stop processing of the events.

But it doesn't.  Focus moves to the next cell anyhow, in spite of the filter. 

>From everything I've read, I'd have thought I was doing this right.  What am I overlooking?

Thanks! 

Since I haven't posted here before, I'm not exactly sure how to include sample code.  I'll try adding them as attachments to the e-mail, but here are links as well:


test.py [http://www.sturtz.org/john/test.py]  [QTableView, Delegate, editor widget and event filter definitions, and __main__]
model.py [http://www.sturtz.org/john/model.py]  [QAbstractTableModel definition, imported by test.py]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190214/cc96d391/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: model.py
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190214/cc96d391/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test.py
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190214/cc96d391/attachment-0001.ksh>


More information about the PyQt mailing list