Gui: add missing PropertyItemDelegate::eventFilter()

For handling focus change when editing property
This commit is contained in:
Zheng, Lei
2022-02-24 12:50:09 +08:00
committed by wmayer
parent d5b032970c
commit 75dd3d6533
2 changed files with 39 additions and 0 deletions

View File

@@ -37,6 +37,8 @@
#include "PropertyItemDelegate.h"
#include "PropertyItem.h"
#include "PropertyEditor.h"
#include "MDIView.h"
#include "Tree.h"
FC_LOG_LEVEL_INIT("PropertyView",true,true)
@@ -124,6 +126,40 @@ bool PropertyItemDelegate::editorEvent (QEvent * event, QAbstractItemModel* mode
return QItemDelegate::editorEvent(event, model, option, index);
}
bool PropertyItemDelegate::eventFilter(QObject *o, QEvent *ev)
{
if (ev->type() == QEvent::FocusOut) {
PropertyEditor *parentEditor = qobject_cast<PropertyEditor*>(this->parent());
auto widget = qobject_cast<QWidget*>(o);
if (widget && parentEditor && parentEditor->activeEditor
&& widget != parentEditor->activeEditor)
{
// We event filter child QAbstractButton and QLabel of an editor,
// which requires special focus change in order to not mess up with
// QItemDelegate's logic.
QWidget *w = QApplication::focusWidget();
// For some reason, Qt (5.15) on Windows will remove current focus
// before bringing up a modal dialog.
if (!w)
return false;
while (w) { // don't worry about focus changes internally in the editor
if (w == widget || w == parentEditor->activeEditor)
return false;
// ignore focus change to 3D view or tree view, because, for
// example DlgPropertyLink is implemented as modeless dialog
// to allow selection in 3D and tree view.
if (qobject_cast<MDIView*>(w))
return false;
if (qobject_cast<TreeWidget*>(w))
return false;
w = w->parentWidget();
}
}
}
return QItemDelegate::eventFilter(o, ev);
}
QWidget * PropertyItemDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & /*option*/,
const QModelIndex & index ) const
{

View File

@@ -44,6 +44,9 @@ public:
virtual QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const;
virtual bool editorEvent (QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem& option, const QModelIndex& index);
protected:
bool eventFilter(QObject *, QEvent *);
public Q_SLOTS:
void valueChanged();