Skip to content

Commit

Permalink
Select text when TimeSpinBox gets focus.
Browse files Browse the repository at this point in the history
  • Loading branch information
ddennedy committed Jan 15, 2015
1 parent e9bf4cf commit 885a6e3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/widgets/timespinbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
TimeSpinBox::TimeSpinBox(QWidget *parent)
: QSpinBox(parent)
{
setLineEdit(new TimeSpinBoxLineEdit);
setRange(0, INT_MAX);
setFixedWidth(this->fontMetrics().width("HHH:MM:SS.MMM"));
setAlignment(Qt::AlignRight);
Expand Down Expand Up @@ -50,3 +51,27 @@ QString TimeSpinBox::textFromValue(int val) const
}
return QString();
}


TimeSpinBoxLineEdit::TimeSpinBoxLineEdit(QWidget *parent)
: QLineEdit(parent)
, m_selectOnMousePress(false)
{
}

void TimeSpinBoxLineEdit::focusInEvent(QFocusEvent *event)
{
QLineEdit::focusInEvent(event);
selectAll();
m_selectOnMousePress = true;
}

void TimeSpinBoxLineEdit::mousePressEvent(QMouseEvent *event)
{
QLineEdit::mousePressEvent(event);
if (m_selectOnMousePress) {
selectAll();
m_selectOnMousePress = false;
}
}

16 changes: 16 additions & 0 deletions src/widgets/timespinbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define TIMESPINBOX_H

#include <QSpinBox>
#include <QLineEdit>

class QRegExpValidator;

Expand All @@ -39,4 +40,19 @@ class TimeSpinBox : public QSpinBox
QRegExpValidator* m_validator;
};

class TimeSpinBoxLineEdit : public QLineEdit
{
Q_OBJECT

public:
explicit TimeSpinBoxLineEdit(QWidget *parent = 0);

protected:
void focusInEvent(QFocusEvent *event);
void mousePressEvent(QMouseEvent *event);

private:
bool m_selectOnMousePress;
};

#endif // TIMESPINBOX_H

0 comments on commit 885a6e3

Please sign in to comment.