-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineEdit.cpp
More file actions
62 lines (56 loc) · 1.46 KB
/
Copy pathLineEdit.cpp
File metadata and controls
62 lines (56 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "LineEdit.h"
#include <QKeyEvent>
#include <QValidator>
LineEdit::LineEdit(QWidget *parent) : QLineEdit(parent)
{}
LineEdit::LineEdit(const QString &str) : QLineEdit(str)
{}
void LineEdit::setValidator(const QValidator *validator)
{
connect(validator, SIGNAL(changed()),
this, SLOT(validatorCanged()));
QLineEdit::setValidator(validator);
}
void LineEdit::validatorCanged()
{
QString input = text(), out;
int pos = 0;
QRegExp regex = qobject_cast<const QRegExpValidator*>(validator())->regExp();
int start = regex.indexIn(input, pos);
int len = input.length();
while(start != -1 && start != len)
{
QString cap =regex.cap();
out += cap;
pos = start + cap.length();
if(cap.length() == 0)
pos++;
start = regex.indexIn(input, pos);
}
setText(out);
}
void LineEdit::keyPressEvent(QKeyEvent *event)
{
int key = event->key();
if(!(event->modifiers() & Qt::ControlModifier) &&
((key >= Qt::Key_0 && key <= Qt::Key_9) ||
(key >= Qt::Key_A && key <= Qt::Key_Z)))
{
QString value = text();
int position = cursorPosition();
if(selectionStart() > -1)
value = value.remove(selectionStart(), selectedText().length());
value = value.insert(position, event->text()).toUpper();
int pos = 0;
if(validator()->validate(value, pos) == QValidator::Acceptable)
{
setText(value);
setCursorPosition(++position);
}
}
else
{
QLineEdit::keyPressEvent(event);
return;
}
}