QT学习笔记之五 HexSpinBox

这个程序主要为了,练习自定义Qt窗体部件,HexSpinBox实现了一个16进制的SpinBox,所以只要重载SpinBox就可以了…详见代码

==================================================================

hexspinbox.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef HEXSPINBOX_H
#define HEXSPINBOX_H
#include <QtGui/QWidget>
#include <QSpinBox>
namespace Ui
{
class HexSpinBoxClass;
}
class HexSpinBox : public QSpinBox //继承QSpinBox 控件
{
Q_OBJECT
public:
HexSpinBox(QWidget *parent = 0);
~HexSpinBox();
protected:
QValidator::State validate(QString &text,int &pos) const;
int valueFromText(const QString &text) const; //virtual函数必须重载
QString textFromValue(int value) const; //virtual函数必须重载
private:
Ui::HexSpinBoxClass *ui;
QRegExpValidator *validator;
};
#endif // HEXSPINBOX_H

hexspinbox.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
#include "hexspinbox.h"
#include "ui_hexspinbox.h"
HexSpinBox::HexSpinBox(QWidget *parent)
:QSpinBox(parent)
{
ui->setupUi(this);
setRange(0,255);
validator=new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"),this);
}
HexSpinBox::~HexSpinBox()
{
delete ui;
}
/*检查用户输入的合法性*/
QValidator::State HexSpinBox::validate(QString &text,int &pos) const
{
return validator->validate(text,pos);
}
/*整型值转换为16进制的字符串*/
QString HexSpinBox::textFromValue(int value) const
{
return QString::number(value,16).toUpper();
}
/*字符串到整数值,若字符串合法ok返回true,否则返回false*/
int HexSpinBox::valueFromText(const QString &text) const
{
bool ok;
return text.toInt(&ok,16);
}

main.cpp

1
2
3
4
5
6
7
8
9
10
#include <QtGui/QApplication>
#include "hexspinbox.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
HexSpinBox *hexSpinBox=new HexSpinBox;
hexSpinBox->sizeHint();
hexSpinBox->show();
return app.exec();
}

image

PS:庆祝空间人气突破25000

坚持原创技术分享,您的支持将鼓励我继续创作!