diff --git a/gallery.cpp b/gallery.cpp index 210821e..eaf9f18 100644 --- a/gallery.cpp +++ b/gallery.cpp @@ -1,5 +1,7 @@ #include "gallery.h" +#include +#include #include #include "qsearchfield.h" @@ -11,8 +13,9 @@ Gallery::Gallery(QWidget *parent) : QWidget(parent) setWindowTitle("Qocoa Gallery"); QVBoxLayout *layout = new QVBoxLayout(this); - QSearchField *searchField = new QSearchField(this); + searchField = new QSearchField(this); layout->addWidget(searchField); + searchField->installEventFilter(this); QSearchField *searchFieldPlaceholder = new QSearchField(this); searchFieldPlaceholder->setPlaceholderText("Placeholder text"); @@ -73,3 +76,20 @@ Gallery::Gallery(QWidget *parent) : QWidget(parent) progressIndicatorSpinning->animate(); layout->addWidget(progressIndicatorSpinning); } + +bool Gallery::eventFilter(QObject *watched, QEvent *event) +{ + if (watched == searchField) { + if (event->type() == QEvent::KeyPress) { + QKeyEvent* e = static_cast(event); + switch (e->key()) { + case Qt::Key_Return: + QMessageBox::information(this, "Key event received", + "Key event \"" + QKeySequence(e->key()).toString() + + "\" received on searchField"); + return true; + } + } + } + return QWidget::eventFilter(watched, event); +} diff --git a/gallery.h b/gallery.h index 1e83bad..435cf74 100644 --- a/gallery.h +++ b/gallery.h @@ -3,12 +3,18 @@ #include +class QSearchField; + class Gallery : public QWidget { Q_OBJECT public: explicit Gallery(QWidget *parent = 0); +protected: + bool eventFilter(QObject *watched, QEvent *event) override; +private: + QSearchField *searchField; }; #endif // WIDGET_H diff --git a/qsearchfield_mac.mm b/qsearchfield_mac.mm index 6704a47..2213aa8 100644 --- a/qsearchfield_mac.mm +++ b/qsearchfield_mac.mm @@ -29,6 +29,7 @@ of this software and associated documentation files (the "Software"), to deal #import "AppKit/NSSearchField.h" #include +#include #include class QSearchFieldPrivate : public QObject @@ -51,8 +52,11 @@ void textDidEndEditing() void returnPressed() { - if (qSearchField) + if (qSearchField) { emit qSearchField->returnPressed(); + QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier); + QApplication::postEvent(qSearchField, event); + } } QPointer qSearchField;