Results 1 to 3 of 3

Thread: Problem with custom QStyledItemDelegate and tab handling

  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Problem with custom QStyledItemDelegate and tab handling

    I have implemented a custom QStyledItemDelegate for editing a cell in a QTableView. The delegate's editor is a QWidget with some radio buttons and two line edits. See the attached screenshots that show the editor widget as a delegate and parented with a QDialog.

    The problem I am having is when the editor is used as a delegate. If I edit the text in either line edit and then press Tab, the editor is immediately closed. The new text is discarded and control goes back to the table view.

    When used in a QDialog, the behavior is normal; Tab moves the focus to the next widget in the tab chain.

    I have installed an event filter on both line edits and watch for FocusOut events. In delegate mode, editing one line edit and then -clicking- on the next generates a focus out event on the first line edit. Editing the first line edit and pressing Tab does not generate a focus out event.

    I have tried setting window modality to Qt::ApplicationModal when using the editor as a delegate. This has no effect on behavior, even though the delegate reports that the editor "isModal" in all of the delegate methods (setEditorData(), setModelData(), updateEditorGeometry() etc.).

    How do I make this editor behave like a modal dialog when used as a delegate? In other words, the user must be forced to click either Ok or Cancel to close the delegate? Do I need to install an event filter on the table view and intercept tabs when the delegate is active?

    AsDelegate.jpg AsDialog.jpg

    EDIT: Installing an event filter on the table view has no effect. After reading more of the QStyledItemDelegate docs, I see that it puts an event filter on the editor which intercepts the Tab key. Overriding this implementation in my derived delegate class has no effect, so it is likely the base class is getting the event first. If I explicitly install my own event filter on the editor class immediately after creating it, there is still no effect, so again, the base class must be getting it first.
    Last edited by d_stranz; 14th April 2020 at 19:39.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    laurynas2003 (16th April 2020)

  3. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    506
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with custom QStyledItemDelegate and tab handling

    Hi, maybe creating the event filter later might help:
    From https://doc.qt.io/qt-5/qobject.html#installEventFilter:
    If multiple event filters are installed on a single object, the filter that was installed last is activated first.
    Ginsengelf

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with custom QStyledItemDelegate and tab handling

    Thanks, I have tried that. I have event filters everywhere - on the line edits, on the widget that contains them, on the delegate itself. I can see the Tab come in (as a QKeyEvent of type Qt:ShortcutOverride), but I can't seem to stop if from propagating and closing the editor. I have overridden eventFilter() on the delegate class (which should force mine to be used instead of the QStyledItemDelegate regardless of which one is installed last). I have even overridden event() on the editor widget to try to trap the event there. Nothing seems to work.

    I discovered the problem by accident. My QLineEdit widgets have a QDoubleValidator installed on them to force entry of a double in a specific range. In this case, the QLineEdit::editingFinished() signal is not emitted (so the docs say, but why not?). I was editing and the data were never stored in the underlying data structure. That's when I discovered my slot was not being called. The other QLineEdit signals are emitted for every character, so they can't be used to retrieve a final value. So I added an event filter on the QLineEdit instances to trap the FocusOut events, and this worked as a substitute for handling editingFinished(). Until I discovered that when I hit Tab to move from one line edit to the next, I never got the FocusOut event on the first line edit.

    I have wasted two days on this so far. I am about to throw it in, make the column in the table view read only, and add a tool tip telling the user if they want to change this field, click the Edit button that lets then edit the whole row in a dialog. Not great, since every other column can be edited in-place, but time is money.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QStyledItemDelegate custom Editor--How to cancel edits
    By davethomaspilot in forum Qt Programming
    Replies: 0
    Last Post: 11th February 2014, 12:43
  2. Replies: 1
    Last Post: 14th June 2011, 03:46
  3. Custom QStyledItemDelegate
    By Berryblue031 in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2011, 10:32
  4. QStyledItemDelegate with custom QWidget editor
    By stefanadelbert in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2010, 23:19
  5. update custom QStyledItemDelegate after drag
    By dano in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2008, 18:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.