From 0c13d610eb0886650e37b7e5a306841685287aad Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 12 Aug 2017 11:57:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=95ext=20in=20QLineEdit=20selected=20on=20ed?= =?UTF-8?q?it=20start.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --HG-- branch : feature --- .../dialogs/support/dialogeditlabel.cpp | 1 - src/libs/vwidgets/vlineedit.cpp | 27 ++++++++++++++++--- src/libs/vwidgets/vlineedit.h | 6 +++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/libs/vtools/dialogs/support/dialogeditlabel.cpp b/src/libs/vtools/dialogs/support/dialogeditlabel.cpp index ec60822b6..14a29f957 100644 --- a/src/libs/vtools/dialogs/support/dialogeditlabel.cpp +++ b/src/libs/vtools/dialogs/support/dialogeditlabel.cpp @@ -85,7 +85,6 @@ void DialogEditLabel::ShowLineDetails() { ui->lineEditLine->blockSignals(true); ui->lineEditLine->setText(line->text()); - ui->lineEditLine->selectAll(); ui->lineEditLine->setFocus(); ui->lineEditLine->blockSignals(false); diff --git a/src/libs/vwidgets/vlineedit.cpp b/src/libs/vwidgets/vlineedit.cpp index fb1d78162..392fa657a 100644 --- a/src/libs/vwidgets/vlineedit.cpp +++ b/src/libs/vwidgets/vlineedit.cpp @@ -30,19 +30,29 @@ //--------------------------------------------------------------------------------------------------------------------- VLineEdit::VLineEdit(QWidget *parent) - : QLineEdit(parent) + : QLineEdit(parent), + m_selectOnMousePress(false) {} //--------------------------------------------------------------------------------------------------------------------- VLineEdit::VLineEdit(const QString &contents, QWidget *parent) - : QLineEdit(contents, parent) + : QLineEdit(contents, parent), + m_selectOnMousePress(false) {} +//--------------------------------------------------------------------------------------------------------------------- +void VLineEdit::focusInEvent(QFocusEvent *e) +{ + QLineEdit::focusInEvent(e); + selectAll(); + m_selectOnMousePress = true; +} + //--------------------------------------------------------------------------------------------------------------------- void VLineEdit::focusOutEvent(QFocusEvent *e) { const int start = selectionStart(); - int selectionLength = selectedText().length(); + const int selectionLength = selectedText().length(); const bool wasTextSelected = hasSelectedText(); QLineEdit::focusOutEvent(e); @@ -53,3 +63,14 @@ void VLineEdit::focusOutEvent(QFocusEvent *e) } } +//--------------------------------------------------------------------------------------------------------------------- +void VLineEdit::mousePressEvent(QMouseEvent *e) +{ + QLineEdit::mousePressEvent(e); + if(m_selectOnMousePress) + { + selectAll(); + m_selectOnMousePress = false; + } +} + diff --git a/src/libs/vwidgets/vlineedit.h b/src/libs/vwidgets/vlineedit.h index 7dd0df160..9b9561608 100644 --- a/src/libs/vwidgets/vlineedit.h +++ b/src/libs/vwidgets/vlineedit.h @@ -33,12 +33,18 @@ class VLineEdit : public QLineEdit { + Q_OBJECT public: VLineEdit(QWidget * parent = nullptr); VLineEdit(const QString &contents, QWidget *parent = nullptr); protected: + virtual void focusInEvent(QFocusEvent *e) Q_DECL_OVERRIDE; virtual void focusOutEvent(QFocusEvent *e) Q_DECL_OVERRIDE; + virtual void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE; +private: + Q_DISABLE_COPY(VLineEdit) + bool m_selectOnMousePress; }; #endif // VLINEEDIT_H