Skip to content

Instantly share code, notes, and snippets.

@hasherezade
Created January 21, 2015 13:24
Show Gist options
  • Select an option

  • Save hasherezade/a1fdf1f9d14f47020fa9 to your computer and use it in GitHub Desktop.

Select an option

Save hasherezade/a1fdf1f9d14f47020fa9 to your computer and use it in GitHub Desktop.
HexSpinBox (subclassed from QSpinBox)
#include "HexSpinBox.h"
HexSpinBox::HexSpinBox(QWidget *parent)
: QSpinBox(parent)
{
validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"), this);
}
QString HexSpinBox::textFromValue(int value) const
{
return QString::number(value, 16).toUpper();
}
int HexSpinBox::valueFromText(const QString &text) const
{
bool ok;
int val = text.toInt(&ok, 16);
if (ok) return val;
return 0;
}
QValidator::State HexSpinBox::validate(QString &text, int &pos) const
{
return validator->validate(text, pos);
}
#pragma once
#include <QtGui>
class HexSpinBox : public QSpinBox
{
Q_OBJECT
public:
HexSpinBox(QWidget *parent = 0);
protected:
QValidator::State validate(QString &text, int &pos) const;
int valueFromText(const QString &text) const;
QString textFromValue(int value) const;
private:
QRegExpValidator *validator;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment