Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Make QSearchField behaves like other QWidget on Mac by sending Qt eve…
Browse files Browse the repository at this point in the history
…nts instead of only widget specific signals
  • Loading branch information
ArnaudBienner committed Sep 23, 2015
1 parent 2affb6a commit d4837b1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
22 changes: 21 additions & 1 deletion gallery.cpp
@@ -1,5 +1,7 @@
#include "gallery.h"

#include <QKeyEvent>
#include <QMessageBox>
#include <QVBoxLayout>

#include "qsearchfield.h"
Expand All @@ -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");
Expand Down Expand Up @@ -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<QKeyEvent*>(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);
}
6 changes: 6 additions & 0 deletions gallery.h
Expand Up @@ -3,12 +3,18 @@

#include <QWidget>

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
6 changes: 5 additions & 1 deletion qsearchfield_mac.mm
Expand Up @@ -29,6 +29,7 @@ of this software and associated documentation files (the "Software"), to deal
#import "AppKit/NSSearchField.h"

#include <QApplication>
#include <QKeyEvent>
#include <QClipboard>

class QSearchFieldPrivate : public QObject
Expand All @@ -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> qSearchField;
Expand Down

0 comments on commit d4837b1

Please sign in to comment.