PDA

View Full Version : QTableWidget editing question



Trasmeister
20th September 2006, 16:55
(Qt 4.1.4)

I've created a standard QTableWidget in a GUI, and I'm using a reimplementation of QItemDelegate so I can provide my own custom mechanism to edit the data contained within that QTableWidget. I'm using QDoubleSpinBox and QComboBox as my custom editing widgets. This all works happily, but my problems stem from defining how the editing is triggered.

What I want to happen is to be able to navigate between the cells (using direction arrows), and kick-off a cell edit by pressing any key, 'Return' or 'Space' keys.


If I set up the following edit trigger flags on the QTableWidget as follows:



QAbstractItemView::DoubleClicked |
QAbstractItemView::SelectedClicked |
QAbstractItemView::EditKeyPressed |
QAbstractItemView::AnyKeyPressed


The behaviour I get is that whenever you enter text, (EG: '1', 'g', 'Z' etc..), the edit is triggered, but editing is NOT triggered on a 'return' or 'space' key stroke.

Alternatively, if I set the edit trigger flags as follows:



QAbstractItemView::AllEditTriggers


I now acheived the desired result of having the edit triggered by a 'space' or 'return', but I also have the undesired result of having any movement onto a new cell triggering an edit. This is a problem because the spin and combo boxes take control of the up and down key strokes; essentially meaning that you can't swiftly or easily navigate the QTableWidget without cancelling the edit (either by 'return' or 'enter').


So, in essence my problem seems to be that I can either have 'Space' and 'Return' start an edit, OR I can have navigation between cells without automatically starting an edit, but not both (which is what I'm looking for!)


While it's possible to hook in to the key press event (either by superclassing QTableWidget, or by installing an event filter), I was wondering if there was a simple way of acheiving the desired behaviour, whether I've done, or missed something rather silly!

Thanks!

-Martin

3dch
20th September 2006, 18:46
Hi Martin

This is just an idea and not based on experience. When you look at the Qt doc of the QItemDelegate class you see that the default implementation of the eventFilter() handles Enter and Return keys to terminate editing:

bool QItemDelegate::eventFilter ( QObject * object, QEvent * event ) [virtual protected]

If the object is the current editor: if the event is an Esc key press the current edit is cancelled and ended, or if the event is an Enter or Return key press the current edit is accepted and ended. If editing is ended the event filter returns true to signify that it has handled the event; in all other cases it does nothing and returns false to signify that the event hasn't been handled.

So instead of trying to make the QTableWidget behave as desired you could try to focus on the item delegate and implement your own eventFilter().

Ernst