-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhexspinbox.cpp
More file actions
executable file
·53 lines (52 loc) · 1.32 KB
/
hexspinbox.cpp
File metadata and controls
executable file
·53 lines (52 loc) · 1.32 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
//#include <QTGui>
#include "hexspinbox.h"
HexSpinBox::HexSpinBox(QWidget *parent)
: QSpinBox(parent)
{
setPrefix("0x");
setDisplayIntegerBase(16);
setRange(INT_MIN,INT_MAX);
//validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"),this);
}
unsigned int HexSpinBox::hexValue() const
{
return u(value());
}
void HexSpinBox::setHexValue(unsigned int value)
{
setValue(i(value));
}
QValidator::State HexSpinBox::validate(QString &text, int &pos) const
{
// return validator->validate(text,pos);
QString copy(text);
if (copy.startsWith("0x"))
copy.remove(0, 2);
pos -= copy.size() - copy.trimmed().size();
copy = copy.trimmed();
if (copy.isEmpty())
return QValidator::Intermediate;
text = QString("0x") + copy.toUpper();
bool okay;
unsigned int val = copy.toUInt(&okay, 16);
if (!okay)
return QValidator::Invalid;
return QValidator::Acceptable;
}
QString HexSpinBox::textFromValue( int value) const
{
return QString::number(u(value),16).toUpper();
}
int HexSpinBox::valueFromText(const QString &text) const
{
bool ok;
return i(text.toInt(&ok,16));
}
inline unsigned int HexSpinBox::u(int i) const
{
return *reinterpret_cast<unsigned int *>(&i);
}
inline int HexSpinBox::i(unsigned int u) const
{
return *reinterpret_cast<int *>(&u);
}