Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

Projektitiedosto

Code Block
TEMPLATE      = app
HEADERS       = hexspinbox.h
SOURCES       = hexspinbox.cpp \
                main.cpp

fffOtsikkotiedosto

Code Block
#ifndef HEXSPINBOX_H
#define HEXSPINBOX_H
#include <QSpinBox>

class QRegExpValidator;//luokan etukateisesittely ettei kaantaja anna virhetta

//tehdääntehdaan oma HexSpinBox luokka ja peritäänperitaan se Qt:n QSpinBoxista
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;
};
#endif

Luokan toteutus

Code Block
#include <QtGui>
#include "hexspinbox.h"

HexSpinBox::HexSpinBox(QWidget *parent)
    : QSpinBox(parent)
{
    setRange(0, 255);
    validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"), this);
}

QValidator::State HexSpinBox::validate(QString &text, int &pos) const
{
    return validator->validate(text, pos);
}

int HexSpinBox::valueFromText(const QString &text) const
{
    bool ok;
    return text.toInt(&ok, 16);
}

QString HexSpinBox::textFromValue(int value) const
{
    return QString::number(value, 16).toUpper();
}

Pääohjelma

Code Block
#include <QApplication>
#include <QHBoxLayout>
#include "hexspinbox.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    HexSpinBox spinBox;
    spinBox.setWindowTitle(QObject::tr("Hex Spin Box"));
    spinBox.show();
    return app.exec();
}