By means of inheritance, subclasses of the original widgets
(1) Improvement Method (Promotion): Create a new project, drag the Qspinbox part into the form in the UI interface, right-click the part. In the pop-up menu, select Promote to. ( Add the following two files to the project )
#ifndef Hexspinbox_h#defineHexspinbox_h#include<QSpinBox>classQregexpvalidator;classHexspinbox: Publicqspinbox{Q_object Public: Hexspinbox (Qwidget*parent);protected: Qvalidator::state Validate (QString&text,int&pos)Const; intValuefromtext (ConstQString &text)Const; QString Textfromvalue (intValConst;Private: Qregexpvalidator*Validator;};#endif //Hexspinbox_h
#include <QtGui>#include"HexSpinBox.h"Hexspinbox::hexspinbox (Qwidget*parent): Qspinbox (parent) {SetRange (0,255);//default range of inputValidator=NewQregexpvalidator (Qregexp ("[0-9a-fa-f]{1,8}"), This);//Accept 1 to 8 characters}qvalidator::state hexspinbox::validate (QString&text,int&pos)Const{ returnValidator->validate (Text,pos);//Check that the input is legal}qstring Hexspinbox::textfromvalue (intValConst{ returnQstring::number (Val, -). ToUpper ();//convert integers to Strings to update spinner boxes}intHexspinbox::valuefromtext (ConstQString &text)Const{ BOOLOK; returnText.toint (&ok, -);//if the string is not a valid hexadecimal number: OK is false;}
(2) Plug-in method (plugin)
customizing Widgets--custom Widgets