Results 1 to 6 of 6

Thread: Custom item delegate gets the wrong editing widget in eventFilter

  1. #1
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Post Custom item delegate gets the wrong editing widget in eventFilter

    So I've implemented a custom text edit widget which only accepts numbers and other math symbols as edit, as well as doing some other stuff with the key press event. My widget works fine when tested on it's own but it seems that when it is used in the delegate that I created it doesn't receive the keypressevents.

    After some research I determined that I was missing a crucial part of my custom delegate implementation. I still needed to implement eventFilter() and return false on a keypress event so that the editor would receive the event. What I discovered while doing this is that my delegate seems to be using a QExpandingLineEdit instead of my custom editor. So even with the event filter, my custom widget is not getting the keypress events. I don't understand what I'm missing from my implementation.

    Qt Code:
    1. MathEditDelegate::MathEditDelegate(QObject *parent) :
    2. QStyledItemDelegate(parent)
    3. {
    4. }
    5.  
    6. void MathEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
    7. if(MathEdit* edit = qobject_cast<MathEdit*>(editor)){
    8. QString currentText = index.data(Qt::DisplayRole | Qt::EditRole).toString();
    9. edit->setHtml(currentText);
    10. }
    11. else
    12. QStyledItemDelegate::setEditorData(editor, index);
    13. }
    14.  
    15. bool MathEditDelegate::eventFilter(QObject *editor, QEvent *event){
    16. if(event->type()==QEvent::KeyPress){
    17. qDebug()<<editor->metaObject()->className(); //This should be MathEdit, but instead is always QExpandingLineEdit
    18. return false;
    19. }
    20. return QStyledItemDelegate::eventFilter(editor, event);
    21. }
    22.  
    23. void MathEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
    24. if(MathEdit* edit = qobject_cast<MathEdit*>(editor)){
    25. model->setData(index, edit->toHtml(), Qt::DisplayRole | Qt::EditRole);
    26. }
    27. else
    28. QStyledItemDelegate::setModelData(editor, model, index);
    29. }
    30.  
    31. QWidget* MathEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &/*index*/){
    32. MathEdit* edit = new MathEdit(parent);
    33. return edit;
    34. }
    To copy to clipboard, switch view to plain text mode 
    I've implemented createEditor so shouldn't the editor in eventFilter be the editor I created and not the default "QExpandingLineEdit"?
    Upon further testing, I noticed that all of my functions with editor as an argument are coming in as QExpandingLineEdits and not MathEdits. I even made a minimal example with just a standard table view and model with the delegate assigned, and I got the same results as I did in the larger project. I have no idea what I'm missing.

    Any insight would be appreciated.


    EDIT:
    Turns out I was just missing the override keyword on my methods. How embarrassing.
    Last edited by forgottenduck; 27th October 2014 at 21:10. Reason: updated contents

  2. #2
    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: Custom item delegate gets the wrong editing widget in eventFilter

    Turns out I was just missing the override keyword on my methods. How embarrassing.
    The methods you show are already defined as virtual in the base classes you inherit from. You shouldn't need to add any keyword to have them treated as such in a derived class, unless C++-11 has turned the semantics of virtual methods on its head.

    And why do you install an event filter? Shouldn't you just reimplement the QAbstractItemDelegate::editorEvent() method instead?

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom item delegate gets the wrong editing widget in eventFilter

    Quote Originally Posted by d_stranz View Post
    The methods you show are already defined as virtual in the base classes you inherit from. You shouldn't need to add any keyword to have them treated as such in a derived class, unless C++-11 has turned the semantics of virtual methods on its head.
    C++11 and onward do not change the behavior of virtual. The override keyword helps to detect when one is not actually overriding, e.g. when there is a signature mismatch or the virtual gets removed from the base class.

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    d_stranz (29th October 2014)

  5. #4
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom item delegate gets the wrong editing widget in eventFilter

    Quote Originally Posted by d_stranz View Post
    And why do you install an event filter? Shouldn't you just reimplement the QAbstractItemDelegate::editorEvent() method instead?
    As I understand editorEvent(), it is the method called when the editor is opened, while eventFilter is used to capture all the events while the editor is open. Using eventFilter() I was able to get the events to pass to my editor widget (after adding override).

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Custom item delegate gets the wrong editing widget in eventFilter

    "override" is a compile time check, it cannot make your software "start working". And you shouldn't need to filter events, the editor will receive key events in its keyPressEvent() implementation.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following 2 users say thank you to wysota for this useful post:

    d_stranz (29th October 2014), forgottenduck (30th October 2014)

  8. #6
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom item delegate gets the wrong editing widget in eventFilter

    It definitely seems like you're right about that. I must have changed something else to get it working because removing override and getting rid of eventFilter(), changed nothing about how the application behaves. Anyway, it's been working as intended so I doubt I'll go back and track down the issue I was having.

    Thanks for your help.

Similar Threads

  1. Replies: 1
    Last Post: 10th May 2011, 22:35
  2. QTreeWidget Custom Item Delegate
    By photo_tom in forum Qt Programming
    Replies: 1
    Last Post: 20th May 2010, 18:59
  3. Replies: 5
    Last Post: 10th August 2009, 10:50
  4. eventFilter: pop-up custom widget
    By vonCZ in forum Newbie
    Replies: 1
    Last Post: 22nd November 2007, 09:54
  5. [Qt4] Noob and custom Item Delegate
    By naresh in forum Newbie
    Replies: 36
    Last Post: 19th March 2006, 15:46

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.