Propagate TAB key from delegate to custom editor



  • Hi to Everyone!
    I'm using a standard model/view pattern and delegates to provide a custom editor. I need to handle the Tab key, in specific I would like to cycle a set of data inside the editor. Unfortunately I'm not able to propagate the keyEvent to the editor, I read that the standard delegate eventFilter handles the Tab key so I reimplemented the bool eventFilter(QObject* , QEvent*)method returning false for the Tab key but the event doesn't reach the custom editor. How can I solve this issue?
    I provide an example code takenfrom the Qt examples - QSpinBoxDelegate, where I modified the file "delegate.cpp"

    class MyEditor : public QSpinBox
    {
    public:
        MyEditor(QWidget* parent = 0)
            : QSpinBox(parent)
        {
    
        }
    
    private:
        void keyPressEvent(QKeyEvent* e)
        {
            if(e->key() == Qt::Key_Tab)
            {
                qDebug() << Q_FUNC_INFO;
            }
            else
            {
                QSpinBox::keyPressEvent(e);
            }
        }
    };
    
    SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
        : QStyledItemDelegate(parent)
    {
    }
    
    bool SpinBoxDelegate::eventFilter(QObject *object, QEvent *event)
    {
        if(event->type() == QEvent::KeyPress)
        {
            QKeyEvent* keyEv = static_cast<QKeyEvent*>(event);
            if(keyEv->key() == Qt::Key_Tab)
            {
                return false;
            }
        }
    
            QStyledItemDelegate::eventFilter(object, event);
    
    }
    
    QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
        const QStyleOptionViewItem &/* option */,
        const QModelIndex &/* index */) const
    {
        MyEditor *editor = new MyEditor(parent);
        editor->setFrame(false);
        editor->setMinimum(0);
        editor->setMaximum(100);
    
        return editor;
    }
    

    Thank you in advance for any suggestions.
    Carlo



  • @CarloEste

    Hello,

    Instead of this

    if(keyEv->key() == Qt::Key_Tab)
         {
             return false;
         }
    

    Try this:

    if(keyEv->key() == Qt::Key_Tab)
         {
             keyEv->ignore();
         }


  • I tried, but it doesn't work.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.