Propagate TAB key from delegate to custom editor
-
wrote on 1 Jun 2016, 15:22 last edited by
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 -
wrote on 1 Jun 2016, 18:27 last edited by
Hello,
Instead of this
if(keyEv->key() == Qt::Key_Tab) { return false; }
Try this:
if(keyEv->key() == Qt::Key_Tab) { keyEv->ignore(); }
-
wrote on 7 Jun 2016, 09:08 last edited by
I tried, but it doesn't work.
-
wrote on 24 Jun 2022, 14:28 last edited by
Hi,
I am experiencing the same. Have you ever sorted this out what filters out the tab from your delegate editor?
-
wrote on 24 Jun 2022, 16:11 last edited by
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)?