Results 1 to 6 of 6

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.