void LCDChanged( const QString& str ); private: int counter;
int fh; // +1,-2,*3,/4,=5 int flag; //0:clear; 1:un-do int ans; }; #endif
>>>CCounter.cpp<<<
#include \
CCounter::CCounter ( QWidget * parent, const char * name, bool modal ) :QDialog( parent, name, modal ) {
m_stateLCD = cDigital; InitButton(); InitConnect();
setMinimumSize( cAppWidth, cAppHeight ); setMaximumSize( cAppWidth, cAppHeight ); }
CCounter::~CCounter() { }
void CCounter::InitConnect(void) // 建立信号和插槽的连接。二者的参数列表要相同 {
QObject::connect( m_pOn, SIGNAL( clicked() ),this, SLOT( ClearLCDNumber() ) ); QObject::connect( m_pCancel, SIGNAL( clicked() ),this, SLOT( DeleteLCDNumber() ) );
QObject::connect( AddButton, SIGNAL( clicked() ),this, SLOT( IncCounter() ) );
QObject::connect( SubButton, SIGNAL( clicked() ),this, SLOT( SubCounter() ) ); QObject::connect( MulButton, SIGNAL( clicked() ),this, SLOT( MulCounter() ) ); QObject::connect( DivButton, SIGNAL( clicked() ),this, SLOT( DivCounter() ) );
QObject::connect( EqlButton, SIGNAL( clicked() ),this, SLOT( Culculate() ) );
int i;
for ( i=0; i<=9; i++ ) {
QObject::connect( m_pDigitalBtn[i], SIGNAL( numChanged( const QString& ) ), this, SLOT( AddLCDNumber( const QString& ) ) ); }
QObject::connect( this, SIGNAL( LCDChanged( const QString& ) ), m_pLCD, SLOT( display( const QString& ) ) ); }
void CCounter::InitButton(void) {
int i=0, j=0; char chNum[4];
m_pLCD = new QLCDNumber( DisLenth , this, \// LCD m_pLCD->setSegmentStyle( QLCDNumber::Flat );
m_pLCD->setGeometry( 0, 0, cAppWidth, iHeightOther ); m_pOn = new QPushButton( \// On Button
m_pOn->setGeometry( 0*iWidthOn, 1*iHeightOther, iWidthOn, iHeightOther ); m_pCancel = new QPushButton( \// Cancel Button
m_pCancel->setGeometry( 1*iWidthOn, 1*iHeightOther, iWidthCancel, iHeightOther );
AddButton = new QPushButton( \
AddButton->setGeometry( 3*iWidthDigital, 2*iHeightOther, iWidthDigital, iHeightDigital );
SubButton = new QPushButton( \
SubButton->setGeometry( 3*iWidthDigital, 2*iHeightOther+1*iHeightDigital, iWidthDigital, iHeightDigital );
MulButton = new QPushButton( \
MulButton->setGeometry( 3*iWidthDigital, 2*iHeightOther+2*iHeightDigital, iWidthDigital, iHeightDigital );
DivButton = new QPushButton( \
DivButton->setGeometry( 3*iWidthDigital, 2*iHeightOther+3*iHeightDigital, iWidthDigital, iHeightDigital );
EqlButton = new QPushButton( \
EqlButton->setGeometry( 2*iWidthDigital, 2*iHeightOther+3*iHeightDigital, iWidthDigital, iHeightDigital );
for( i=0; i<=9; i++ ) // Digital Buttons { sprintf( chNum, \ m_pDigitalBtn[i] = new CDigitalButton( chNum, this ); QString s( chNum ); m_pDigitalBtn[i]->m_strNum = s; }
for( i=0; i<3; i++ ) // 3 rows for digital buttons from 1 to 9
{ for( j=0; j<3; j++ ) { m_pDigitalBtn[i*3+j+1]->setGeometry( j*iWidthDigital, 2*iHeightOther+i*iHeightDigital, iWidthDigital, iHeightDigital ); } }
m_pDigitalBtn[0]->setGeometry( 0*iWidthDigital, 2*iHeightOther+3*iHeightDigital, iWidthDigital, iHeightDigital ); ans=0; fh=0; flag=1; }
void CCounter::IncCounter() {
int num; bool ok;
if ( flag != 0) {
num = m_strLCD.toInt( &ok , 10 ); if ( fh ==1 ) ans += num;
if ( fh ==2 ) ans = ans-num; if ( fh ==3 ) ans = ans*num; if ( fh ==4 ) ans = ans/num; if ( fh ==0 ) ans = num; flag=0; fh=1;
m_strLCD = m_strLCD.setNum( ans ); emit LCDChanged( m_strLCD ); } }
void CCounter::SubCounter() {
int num; bool ok;
if ( flag != 0) {
num = m_strLCD.toInt( &ok, 10 );
if ( fh ==1 ) ans += num;
if ( fh ==2 ) ans = ans-num; if ( fh ==3 ) ans = ans*num; if ( fh ==4 ) ans = ans/num; if ( fh ==0 ) ans = num; flag=0; fh=2;
m_strLCD = m_strLCD.setNum( ans ); emit LCDChanged( m_strLCD ); } }
void CCounter::MulCounter() {
int num; bool ok;
if ( flag != 0) {
num = m_strLCD.toInt( &ok, 10 ); if ( fh ==1 ) ans += num;
if ( fh ==2 ) ans = ans-num; if ( fh ==3 ) ans = ans*num; if ( fh ==4 ) ans = ans/num; if ( fh ==0 ) ans = num;
flag=0; fh=3;
m_strLCD = m_strLCD.setNum( ans ); emit LCDChanged( m_strLCD ); } }
void CCounter::DivCounter() {
int num; bool ok;
if ( flag !=0 ) {
num = m_strLCD.toInt( &ok , 10 );
if ( fh ==1 ) ans += num;
if ( fh ==2 ) ans = ans-num; if ( fh ==3 ) ans = ans*num; if ( fh ==4 ) ans = ans/num; if ( fh ==0 ) ans = num;
flag=0; fh=4;
m_strLCD = m_strLCD.setNum( ans ); emit LCDChanged( m_strLCD ); } }
void CCounter::Culculate() {
int num; bool ok;
num = m_strLCD.toInt( &ok , 10 );
if ( flag != 0) {
if ( fh ==1 ) ans += num; if ( fh ==2 ) ans = ans-num; if ( fh ==3 ) ans = ans*num; if ( fh ==4 ) ans = ans/num; if ( fh ==0 ) ans = num; fh=0;
m_strLCD = m_strLCD.setNum( ans ); emit LCDChanged( m_strLCD ); } }
void CCounter::ClearLCDNumber() {
int len = m_strLCD.length(); if( len < DisLenth ) { flag=1; fh=0; ans=0; m_strLCD.truncate( 0 ); emit LCDChanged( m_strLCD ); // 发射信号