[Solved][Spreadsheet] What is eating LineEdit's key presses?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
chennes
Veteran
Posts: 3881
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

[Solved][Spreadsheet] What is eating LineEdit's key presses?

Post by chennes »

Spreadsheet has a subclass of ExpressionLineEdit that's called LineEdit. It reimplements the Qt event(QEvent *event) function, and has code in it that looks like it's handling key presses of Tab and Enter. But if I put breakpoints in there, it does not appear that code is ever being hit. In fact, there's a bunch of code in Spreadsheet connected to the line edit's "returnPressed()" signal that as far as I can tell is never called. Can anyone clue me into what's going on here? When is this code run? Where are LineEdit::event()'s key presses going?
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: [Spreadsheet] What is eating LineEdit's key presses?

Post by openBrain »

chennes wrote: Mon Oct 04, 2021 3:40 am Spreadsheet has a subclass of ExpressionLineEdit that's called LineEdit. It reimplements the Qt event(QEvent *event) function, and has code in it that looks like it's handling key presses of Tab and Enter. But if I put breakpoints in there, it does not appear that code is ever being hit. In fact, there's a bunch of code in Spreadsheet connected to the line edit's "returnPressed()" signal that as far as I can tell is never called. Can anyone clue me into what's going on here? When is this code run? Where are LineEdit::event()'s key presses going?
IMO this is eaten here : https://github.com/FreeCAD/FreeCAD/blob ... #L420-L447
Notice this is in general a bad thing to reimplement the general 'event()' except for very special needs.
It is far better to reimplement :
* methods called by events : in this case, something like 'focusNextPrevChild' for tab and 'editingFinished' for Enter/Return
* specific events if methods aren't available/appropriate : in this case, 'keyPressEvent'

HTH
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: [Spreadsheet] What is eating LineEdit's key presses?

Post by wmayer »

When you create a spreadsheet then right to "Content:" you will see a LineEdit. When pressing Tab or Enter then it calls LineEdit::event.

When you double-click on a cell in the sheet view then the edit widget also is a LineEdit. When you press Enter then the event is also handled by LineEdit::event. Only when you press Tab the event is handled in SheetTableView::event as a QEvent::ShortcutOverride but then I guess it's further handled inside QTableView::event and doesn't come through SheetTableView::event as a KeyEvent any more.
User avatar
chennes
Veteran
Posts: 3881
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: [Spreadsheet] What is eating LineEdit's key presses?

Post by chennes »

In my testing I put breakpoints in SheetTableView::event() and LineEdit::event():
  • If you are not editing a cell, but just pressing the navigation keys to move around the sheet, SheetTableView::event() gets called, and handles the navigation via the enter and tab keys.
  • If you are editing a cell, LineEdit::event() gets normal key press events for letters, numbers, etc. but doesn't do anything with them, just passes them onto its superclass.
  • If you are editing a cell, LineEdit::event() does not get any events that confirm an edit (tab, enter, return, etc.). Neither does SheetTableView::event(). Something else is handling this.
As far as I can tell, Enter and Tab events are never getting to the LineEdit class's event() function, and they only get to SheetTableView if you are not editing a cell. There's a whole chunk of code in SpreadSheetView that as far as I can tell is never called:

These connections:

connect(ui->cellContent, SIGNAL(returnPressed()), this, SLOT( editingFinished() ));
connect(ui->cellAlias, SIGNAL(returnPressed()), this, SLOT( editingFinished() ));

don't seem to be getting triggered, ever, and I couldn't ever get a breakpoint in editingFinished() to trip.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: [Spreadsheet] What is eating LineEdit's key presses?

Post by openBrain »

chennes wrote: Mon Oct 04, 2021 2:58 pm
  • If you are editing a cell, LineEdit::event() does not get any events that confirm an edit (tab, enter, return, etc.). Neither does SheetTableView::event(). Something else is handling this.
As far as I can tell, Enter and Tab events are never getting to the LineEdit class's event() function, and they only get to SheetTableView if you are not editing a cell. There's a whole chunk of code in SpreadSheetView that as far as I can tell is never called:
LineEdit::event() receives the Enter/Return keys.
But not Tab. Regarding it, I'm afraid it's consumed in somewhere like QGuiApplication::notify() or other native function...
User avatar
chennes
Veteran
Posts: 3881
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: [Spreadsheet] What is eating LineEdit's key presses?

Post by chennes »

I'm going to answer my own question (actually, just elaborate on openBrain's solution, which is correct) -- In Qt applications, the Tab key is magic. It gets grabbed at the most core QApplication event-handling level, which handles using it to switch between widgets. If you want to grab this event, or do anything special with the Tab key, in almost all circumstances you need to install an event filter onto QApplication. For obvious reasons, this should be as lightweight as possible, installed only when needed, and removed when it's not required. And your code better not do anything stupid with it, because users expect their tab key to move them between widgets!
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
Post Reply