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::eventFilter
按下Tab
键时,只有QEvent::ShortcutOverride
个事件,但按下任何其他键都会发出QEvent::ShortcutOverride
和QEvent::KeyPress
个事件?
UPD:我希望通过按标准小部件Tab
和Backtab
来实现行之间的移动。
答案 0 :(得分:13)
好吧,最后我做了一些研究。
当视图调用委托的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
。在widget的构造函数中,它为行编辑设置焦点代理:
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)
使用QKeyEvent
代替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();
}
......