From 7ce4dd6f618b64341c9d9520dfc98ede5a7500a7 Mon Sep 17 00:00:00 2001 From: Rosemary Witchaven <32779090+in1tiate@users.noreply.github.com> Date: Mon, 30 Aug 2021 21:23:11 -0500 Subject: Use event filters instead of subclassing QLineEdit and QPlainTextEdit (#587) * replace aolineedit and aotextedit with event filters * use a button to make evidence editable instead of double click --- include/eventfilters.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 include/eventfilters.h (limited to 'include/eventfilters.h') diff --git a/include/eventfilters.h b/include/eventfilters.h new file mode 100644 index 00000000..7847608d --- /dev/null +++ b/include/eventfilters.h @@ -0,0 +1,36 @@ +#ifndef EVENTFILTERS_H +#define EVENTFILTERS_H + +#include +#include + +class AOLineEditFilter : public QObject +{ + Q_OBJECT +public: + bool preserve_selection = false; + +protected: + bool eventFilter(QObject *obj, QEvent *event) override { + QLineEdit *lineEdit = qobject_cast(obj); + if (event->type() == QEvent::FocusOut && lineEdit != nullptr && preserve_selection) { // lost focus + int start = lineEdit->selectionStart(); + #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) + int len = lineEdit->selectionLength(); + #else + int len = lineEdit->selectedText().length(); + #endif + if (start != -1 && len != -1) { + lineEdit->setSelection(start, len);\ + return true; + } + } + return false; + } +signals: + void double_clicked(); +}; + + + +#endif // EVENTFILTERS_H -- cgit