好吧,最后我对此做了一些研究。
解释
当视图调用createEditor
委托的功能时,它还会将委托事件过滤器安装到编辑器。
QWidget *QAbstractItemViewPrivate::editor(const QModelIndex &index,
const QStyleOptionViewItem &options)
{
Q_Q(QAbstractItemView);
QWidget *w = editorForIndex(index).widget.data();
if (!w) {
QAbstractItemDelegate *delegate = delegateForIndex(index);
if (!delegate)
return 0;
w = delegate->createEditor(viewport, options, index);
if (w) {
w->installEventFilter(delegate);
......
}
但是,委托只能捕获编辑器小部件的事件,但不能捕获其子小部件的事件。当Tab
按键被按下时,该QWidget::event
函数被调用,它使用它将焦点更改为另一个小部件:
bool QWidget::event(QEvent *event)
{
......
switch (event->type()) {
......
case QEvent::KeyPress: {
QKeyEvent *k = (QKeyEvent *)event;
bool res = false;
if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) { //### Add MetaModifier?
if (k->key() == Qt::Key_Backtab
|| (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier)))
res = focusNextPrevChild(false);
else if (k->key() == Qt::Key_Tab)
res = focusNextPrevChild(true);
if (res)
break;
}
......
}
......
}
因此,在我的情况下,焦点设置为下一个QPushButton
,QLineEdit
并且事件不会传播到父级(LineEditor
)。
求解
解决问题的正确方法就是这样QSpinBox
做。因为它也有QLineEdit
。在小部件的构造函数中,它为行编辑设置焦点代理:
edit->setFocusProxy(this);
所以所有的焦点事件都会到达主小部件。此外,focusPolicy
必须设置该属性,因为它是NoFocus
默认设置:
setFocusPolicy(Qt::WheelFocus);
此时我们需要做的就是将必要的事件QLineEdit
从主小部件传播到如下所示:
bool LineEditor::event(QEvent *e)
{
switch(e->type())
{
case QEvent::ShortcutOverride:
if(m_lineEdit->event(e))
return true;
break;
case QEvent::InputMethod:
return m_lineEdit->event(e);
default:
break;
}
return QWidget::event(e);
}
void LineEditor::keyPressEvent(QKeyEvent *e)
{
m_lineEdit->event(e);
}
void LineEditor::mousePressEvent(QMouseEvent *e)
{
if(e->button() != Qt::LeftButton)
return;
e->ignore();
}
void LineEditor::mouseReleaseEvent(QMouseEvent *e)
{
e->accept();
}
void LineEditor::focusInEvent(QFocusEvent *e)
{
m_lineEdit->event(e);
QWidget::focusInEvent(e);
}
void LineEditor::focusOutEvent(QFocusEvent *e)
{
m_lineEdit->event(e);
QWidget::focusOutEvent(e);
}
这应该足够了。
棘手
正如上面所说,代表无法捕捉到编辑孩子的事件。因此,要使编辑器的行为像“原生”,我必须将事件从子级复制到编辑器。
LineEditor
在构造函数中安装事件过滤器QLineEdit
:
edit->installEventFilter(this);
过滤器的实现如下所示:
bool LineEditor::eventFilter(QObject *object, QEvent *event)
{
if(event->type() == QEvent::KeyPress)
{
QKeyEvent* keyEvent = static_cast<QKeyEvent *>(event);
if(keyEvent->key() == Qt::Key_Tab || keyEvent->key() == Qt::Key_Backtab)
{
QApplication::postEvent(this, new QKeyEvent(keyEvent->type(), keyEvent->key(), keyEvent->modifiers()));
// Filter this event because the editor will be closed anyway
return true;
}
}
else if(event->type() == QEvent::FocusOut)
{
QFocusEvent* focusEvent = static_cast<QFocusEvent *>(event);
QApplication::postEvent(this, new QFocusEvent(focusEvent->type(), focusEvent->reason()));
// Don't filter because focus can be changed internally in editor
return false;
}
return QWidget::eventFilter(object, event);
}
也可以使用qApp->notify(this, event)
forQKeyEvent
而不是,QApplication::postEvent
因为无论如何我们都会过滤此事件。但这是不可能的,QFocusEvent
因为notify
它将重定向事件并且它不会到达孩子。
请注意,标准(QItemDelegate
或QStyledItemDelegate
)委托将关心焦点在自身内部更改时的情况:
if (event->type() == QEvent::FocusOut || (event->type() == QEvent::Hide && editor->isWindow())) {
//the Hide event will take care of he editors that are in fact complete dialogs
if (!editor->isActiveWindow() || (QApplication::focusWidget() != editor)) {
QWidget *w = QApplication::focusWidget();
while (w) { // don't worry about focus changes internally in the editor
if (w == editor)
return false;
w = w->parentWidget();
}
......