Results 1 to 2 of 2

Thread: TextEdit created by QAbstractItemDelegate can't receive Escape key event?

  1. #1
    Join Date
    Feb 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default TextEdit created by QAbstractItemDelegate can't receive Escape key event?

    Hi guys,

    I have a QTableView to display a bunch of data, which is stored in a QStyledItemDelegate. I also want to edit the data sometime, so I use a QAbstractItemDelegate to create an editor (of QTextEdit type) when I double click on the cell. However, I want to handle the key Press event myself, but I never catch Key_Escape pressing in the Text Edit editor (Other keys such as Enter and Ascii can be captured). I checked the code, and found that Escape is directly connected to QTableView's closeEditor() function, which is a virtual method and is automatically invoked. As far as I know, key press event is the bottom layer of event handling, but it is not true with such case.

    I do need to capture the Escape key press event so that I can handle it myself, can anyone tell me how to do this? Thanks!
    Last edited by quantity; 8th November 2011 at 09:32.

  2. #2
    Join Date
    Feb 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TextEdit created by QAbstractItemDelegate can't receive Escape key event?

    I got the answer, and I think would help others:

    Override QStyledItemDelegate::eventFilter() method:

    MyItemDelegate::eventFilter(QObject* editor, QEvent* event)
    {
    if(event->type()==QEvent::KeyPress)
    return false;
    return QStyledItemDelegate::eventFilter(editor, event);
    }
    According to Qt's documentation, QStyledItemDelegate::eventFilter() returns true if the given editor is a valid QWidget and the given event is handled; otherwise returns false. Tab, Backtab, Enter, Return and Ecs are handled by default. So, if you want to handle the key press event yourself, you must let eventFilter return false when KeyPress event occurs. So that the editor's keyPressEvent() method will be invoked instead.

Similar Threads

  1. Replies: 2
    Last Post: 11th August 2011, 10:55
  2. QGraphicsItem can't receive mouse leave/enter event
    By wshn13 in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2011, 08:32
  3. Do the layout class can receive the mouse event?
    By tszzp in forum Qt Programming
    Replies: 3
    Last Post: 23rd December 2009, 08:07
  4. Replies: 6
    Last Post: 3rd September 2008, 14:27
  5. Catching escape event with QMdiSubWindow
    By y.shan in forum Qt Programming
    Replies: 0
    Last Post: 3rd September 2008, 13:02

Tags for this Thread

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.