[Solved][Spreadsheet] What is eating LineEdit's key presses?
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
[Solved][Spreadsheet] What is eating LineEdit's key presses?
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?
Re: [Spreadsheet] What is eating LineEdit's key presses?
IMO this is eaten here : https://github.com/FreeCAD/FreeCAD/blob ... #L420-L447chennes 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?
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
Re: [Spreadsheet] What is eating LineEdit's key presses?
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.
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.
Re: [Spreadsheet] What is eating LineEdit's key presses?
In my testing I put breakpoints in SheetTableView::event() and LineEdit::event():
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.
- 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.
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.
Re: [Spreadsheet] What is eating LineEdit's key presses?
LineEdit::event() receives the Enter/Return keys.chennes wrote: ↑Mon Oct 04, 2021 2:58 pmAs 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:
- 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.
But not Tab. Regarding it, I'm afraid it's consumed in somewhere like QGuiApplication::notify() or other native function...
Re: [Spreadsheet] What is eating LineEdit's key presses?
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!