8

背景

我制作了一个自定义小部件,其中有QLineEdit几个QPushButtons可以与自定义项目委托一起使用:

class LineEditor : public QWidget
{
public:
    explicit LineEditor(QWidget *parent = 0) : QWidget(parent) {
        setLayout(new QHBoxLayout);

        layout()->setContentsMargins(0, 0, 0, 0);
        layout()->setSpacing(0);

        QLineEdit *edit = new QLineEdit(this);
        layout()->addWidget(edit);
        layout()->addWidget(new QPushButton(this));
        layout()->addWidget(new QPushButton(this));

        setFocusProxy(edit);
    }
};

class PropertyDelegate : public QItemDelegate
{
public:
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
        return new LineEditor(parent);
    }

    bool eventFilter(QObject *object, QEvent *event) {
        if (event->type() == QEvent::KeyPress) {
            qDebug() << "KeyPress";
        }
        if (event->type() == QEvent::ShortcutOverride) {
            qDebug() << "ShortcutOverride";
        }

        return QItemDelegate::eventFilter(object, event);
    }
};

我将用这样的方式绑定QListView它们QStandardItemModel

QStandardItemModel *model = new QStandardItemModel;
model->appendRow(new QStandardItem("1"));
model->appendRow(new QStandardItem("2"));
model->appendRow(new QStandardItem("3"));

QListView w;
w.setItemDelegate(new PropertyDelegate);
w.setModel(model);
w.show();

问题

为什么在按下键PropertyDelegate::eventFilterTab只有QEvent::ShortcutOverride事件,但按下任何其他键都会发出QEvent::ShortcutOverrideQEvent::KeyPress事件?

UPD:我想通过按标准小部件Tab来实现行之间的移动。Backtab

4

1 回答 1

16

好吧,最后我对此做了一些研究。

解释

当视图调用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;
        }

        ......
    }

    ......
}

因此,在我的情况下,焦点设置为下一个QPushButtonQLineEdit并且事件不会传播到父级(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它将重定向事件并且它不会到达孩子。

请注意,标准(QItemDelegateQStyledItemDelegate)委托将关心焦点在自身内部更改时的情况:

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();
            }

    ......
于 2013-06-15T18:46:08.173 回答