KWidgetsAddons

klineediteventhandler.cpp
1/*
2 SPDX-FileCopyrightText: 2020-2023 Laurent Montel <montel@kde.org>
3 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "klineediteventhandler.h"
8#include "klineediturldropeventfilter.h"
9
10#include <QKeyEvent>
11#include <QLineEdit>
12
13class LineEditCatchReturnKey : public QObject
14{
16public:
17 explicit LineEditCatchReturnKey(QLineEdit *lineEdit);
18
19protected:
20 bool eventFilter(QObject *obj, QEvent *event) override;
21
22private:
23 QLineEdit *const m_lineEdit;
24};
25
26LineEditCatchReturnKey::LineEditCatchReturnKey(QLineEdit *lineEdit)
27 : QObject(lineEdit)
28 , m_lineEdit(lineEdit)
29{
30 m_lineEdit->installEventFilter(this);
31}
32
33bool LineEditCatchReturnKey::eventFilter(QObject *obj, QEvent *event)
34{
35 if (obj == m_lineEdit) {
36 if (event->type() == QEvent::KeyPress) {
37 auto e = static_cast<QKeyEvent *>(event);
38 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
39 const bool stopEvent = (e->modifiers() == Qt::NoButton || e->modifiers() == Qt::KeypadModifier);
40 if (stopEvent) {
41 Q_EMIT m_lineEdit->returnPressed();
42 }
43 return true;
44 }
45 }
46 }
47 return QObject::eventFilter(obj, event);
48}
49
51{
52 if (auto le = qobject_cast<QLineEdit *>(lineEdit)) {
53 new LineEditCatchReturnKey(le);
54 }
55}
56
58{
59 auto filter = new KLineEditUrlDropEventFilter(lineEdit);
60 lineEdit->installEventFilter(filter);
61}
62
63#include "klineediteventhandler.moc"
This class provides an event filter that can be installed on a QLineEdit or a subclass of it (KLineEd...
void catchReturnKey(QObject *lineEdit)
Do not propagate Return or Enter key presses in line edits.
void handleUrlDrops(QObject *lineEdit)
Handle drop events with URLs and replace the current line edit with the dropped URL.
void returnPressed()
Q_EMITQ_EMIT
Q_OBJECTQ_OBJECT
virtual bool event(QEvent *e)
virtual bool eventFilter(QObject *watched, QEvent *event)
void installEventFilter(QObject *filterObj)
Key_Return
KeypadModifier
NoButton
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.