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 thebool 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 -
Hello,
Instead of this
if(keyEv->key() == Qt::Key_Tab) { return false; }
Try this:
if(keyEv->key() == Qt::Key_Tab) { keyEv->ignore(); }
-
Just to understand, is the delegate (assume it's a delegate with 2 line edits) in editing mode (e.g. after you type in the first line edit you want to go in the second) or just in paint mode (e.g. you want to press tab and go in the first lineedit of the following cell)?