/
myphonelineeditdelegate.cpp
84 lines (51 loc) · 2.17 KB
/
myphonelineeditdelegate.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "myphonelineeditdelegate.h"
MyPhoneLineEditDelegate::MyPhoneLineEditDelegate()
{
}
QWidget *MyPhoneLineEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QLineEdit *editor = new QLineEdit(parent);
QRegExp regexPhone("^([0-9\(\)\/\+ \-]*)$" );
QObject::connect(editor, SIGNAL(textChanged(QString)), this, SLOT(checkInput(QString)));
QRegExpValidator *phoneval = new QRegExpValidator(regexPhone, parent);
editor->setValidator(phoneval);
return editor;
}
void MyPhoneLineEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString value = index.model()->data(index,Qt::EditRole).toString();
QLineEdit *lineedit = static_cast<QLineEdit*>(editor);
lineedit->setText(value);
}
void MyPhoneLineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QLineEdit *lineedit = static_cast<QLineEdit*>(editor);
QString value = lineedit->text();
model->setData(index,value,Qt::EditRole);
}
void MyPhoneLineEditDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
void MyPhoneLineEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QString value = index.model()->data(index,Qt::DisplayRole).toString();
painter->save();
QLabel *ql = new QLabel();
ql->setTextFormat(Qt::RichText);
ql->setTextInteractionFlags(Qt::TextBrowserInteraction);
ql->setAlignment(Qt::AlignCenter);
ql->setText( "<a href=\"callto:"+value+"\" target=\"_top\">"+value+"</a>" );
ql->render(painter, painter->deviceTransform().map(QPoint(option.rect.left()+5,option.rect.center().y()-10)));
painter->restore();
}
void MyPhoneLineEditDelegate::checkInput(QString text)
{
QLineEdit *lineedit = static_cast<QLineEdit*>(this->sender());
QString color;
color = "#f6989d";
if (lineedit->hasAcceptableInput() == true) {
color = "#c4df9b" ;
}
lineedit->setStyleSheet("QLineEdit { background-color: "+color+" }");
}