标签:
通过继承的手段,子类原有的窗口部件
(1)改进法(promotion):新建一个项目,在UI界面拖QSpinBox部件到窗体中,右键点击部件。在弹出菜单中,选择“提升为”。(把下面两个文件加到项目中)
#ifndef HEXSPINBOX_H #define HEXSPINBOX_H #include <QSpinBox> class QRegExpValidator; class HexSpinBox:public QSpinBox { Q_OBJECT public: HexSpinBox(QWidget *parent); protected: QValidator::State validate(QString &text,int &pos) const; int valueFromText(const QString &text) const; QString textFromValue(int val) const; private: QRegExpValidator *validator; }; #endif // HEXSPINBOX_H
#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); //接受1到8个字符 } QValidator::State HexSpinBox::validate(QString &text, int &pos) const { return validator->validate(text,pos); //检查输入是否合法 } QString HexSpinBox::textFromValue(int val) const { return QString::number(val,16).toUpper(); //把整数转成字符串,用来更新微调框 } int HexSpinBox::valueFromText(const QString &text) const { bool ok; return text.toInt(&ok,16); //如果字符串不是有效的十六进制数:ok为false; }
(2)插件法(plugin)
标签:
原文地址:http://www.cnblogs.com/jingjingdidunhe/p/4842342.html