axylp123的个人主页

http://www.qtcn.org/bbs/u/154703  [收藏] [复制]

axylp123

  • 1

    关注

  • 1

    粉丝

  • 16

    访客

  • 等级:新手上路
  • 总积分:15
  • 男,1987-10-10

最后登录:2017-06-01

更多资料

日志

5.1 自定义Qt窗口部件

2015-11-27 21:48
//HexSpinBox.cpp
#ifndef HEXSPINBOX_H
#define HEXSPINBOX_H
#include <QSpinBox>
class HexSpinBox : public QSpinBox
{
    Q_OBJECT
public:
    explicit HexSpinBox(QWidget *parent = 0);
protected:
    QValidator::State validate(QString &input, int &pos) const;
    int valueFromText(const QString &text) const;
    QString textFromValue(int val) const;
signals:
public slots:
private:
    QRegExpValidator *m_pValidator;
};
#endif // HEXSPINBOX_H

//HexSpinBox.cpp
#include <QtGui>
#include "HexSpinBox.h"
HexSpinBox::HexSpinBox(QWidget *parent) :
    QSpinBox(parent),
    m_pValidator(new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"), this))
{
    setRange(0, 255);
}
//返回输入值是否有效
//Invalid       无效
//Intermediate  部分有效
//Acceptable    有效
QValidator::State HexSpinBox::validate(QString &input, int &pos) const
{
    return m_pValidator->validate(input, pos);
}
//字符串转为数值
//在输入框输入时被调用
int HexSpinBox::valueFromText(const QString &text) const
{
    bool ok;
    return text.toInt(&ok, 16);
}
//数值转为字符串
//点击微调框上下按钮时被调用
QString HexSpinBox::textFromValue(int val) const
{
    return QString::number(val, 16).toUpper();
}

//main.cpp
#include <QApplication>
#include "HexSpinBox.h"
int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    HexSpinBox spinBox;
    spinBox.show();
    return app.exec();
}

分类:C++GUIQt4|回复:0|浏览:1090|全站可见|转载
 

下一篇:

上一篇: 2.5 动态对话框

Powered by phpwind v8.7 Certificate Copyright Time now is:04-29 00:52
©2005-2016 QTCN开发网 版权所有 Gzip disabled