Linux下Qt版簡易計算器

說明:本文參考至csdn上的一篇博客,很抱歉的是忘了鏈接地址了!可憐

剛學Qt,想做點簡單的東西,於是參考別人的代碼寫了個Qt版簡易計算器。

開發環境:

|- Linux環境,redhat Enterprise5.4

|- Qt4.2


代碼如下:

calculator.ui

<ui version="4.0" >
 <class>calForm</class>
 <widget class="QWidget" name="calForm" >
  <property name="windowModality" >
   <enum>Qt::NonModal</enum>
  </property>
  <property name="geometry" >
   <rect>
    <x>0</x>
    <y>0</y>
    <width>282</width>
    <height>404</height>
   </rect>
  </property>
  <property name="windowTitle" >
   <string>計算器</string>
  </property>
  <property name="windowIcon" >
   <iconset>image/calculator.PNG</iconset>
  </property>
  <property name="autoFillBackground" >
   <bool>true</bool>
  </property>
  <widget class="QPushButton" name="_6Btn" >
   <property name="geometry" >
    <rect>
     <x>5</x>
     <y>252</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>6</string>
   </property>
  </widget>
  <widget class="QPushButton" name="_4Btn" >
   <property name="geometry" >
    <rect>
     <x>142</x>
     <y>252</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>4</string>
   </property>
  </widget>
  <widget class="QPushButton" name="_3Btn" >
   <property name="geometry" >
    <rect>
     <x>5</x>
     <y>303</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>3</string>
   </property>
  </widget>
  <widget class="QPushButton" name="_2Btn" >
   <property name="geometry" >
    <rect>
     <x>72</x>
     <y>303</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>2</string>
   </property>
  </widget>
  <widget class="QPushButton" name="_1Btn" >
   <property name="geometry" >
    <rect>
     <x>142</x>
     <y>303</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>1</string>
   </property>
  </widget>
  <widget class="QPushButton" name="_0Btn" >
   <property name="geometry" >
    <rect>
     <x>5</x>
     <y>359</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>0</string>
   </property>
  </widget>
  <widget class="QPushButton" name="dotBtn" >
   <property name="geometry" >
    <rect>
     <x>72</x>
     <y>359</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>.</string>
   </property>
  </widget>
  <widget class="QPushButton" name="_8Btn" >
   <property name="geometry" >
    <rect>
     <x>72</x>
     <y>201</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>8</string>
   </property>
  </widget>
  <widget class="QPushButton" name="_5Btn" >
   <property name="geometry" >
    <rect>
     <x>72</x>
     <y>252</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>5</string>
   </property>
  </widget>
  <widget class="QPushButton" name="_9Btn" >
   <property name="geometry" >
    <rect>
     <x>5</x>
     <y>201</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>9</string>
   </property>
  </widget>
  <widget class="QPushButton" name="_7Btn" >
   <property name="geometry" >
    <rect>
     <x>142</x>
     <y>201</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>7</string>
   </property>
  </widget>
  <widget class="QPushButton" name="backBtn" >
   <property name="geometry" >
    <rect>
     <x>209</x>
     <y>108</y>
     <width>61</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text" >
    <string><-</string>
   </property>
  </widget>
  <widget class="QPushButton" name="negBtn" >
   <property name="geometry" >
    <rect>
     <x>142</x>
     <y>359</y>
     <width>51</width>
     <height>27</height>
    </rect>
   </property>
   <property name="text" >
    <string>-/+</string>
   </property>
  </widget>
  <widget class="QPushButton" name="plusBtn" >
   <property name="geometry" >
    <rect>
     <x>210</x>
     <y>151</y>
     <width>61</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text" >
    <string>+</string>
   </property>
  </widget>
  <widget class="QPushButton" name="sqrtBtn" >
   <property name="geometry" >
    <rect>
     <x>140</x>
     <y>151</y>
     <width>51</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text" >
    <string>Sqrt</string>
   </property>
  </widget>
  <widget class="QPushButton" name="modBtn" >
   <property name="geometry" >
    <rect>
     <x>70</x>
     <y>151</y>
     <width>51</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text" >
    <string>Mod</string>
   </property>
  </widget>
  <widget class="QPushButton" name="calBtn" >
   <property name="geometry" >
    <rect>
     <x>142</x>
     <y>108</y>
     <width>51</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text" >
    <string>C</string>
   </property>
  </widget>
  <widget class="QPushButton" name="minusBtn" >
   <property name="geometry" >
    <rect>
     <x>211</x>
     <y>201</y>
     <width>61</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text" >
    <string>-</string>
   </property>
  </widget>
  <widget class="QPushButton" name="multiBtn" >
   <property name="geometry" >
    <rect>
     <x>211</x>
     <y>248</y>
     <width>61</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text" >
    <string>*</string>
   </property>
  </widget>
  <widget class="QPushButton" name="divideBtn" >
   <property name="geometry" >
    <rect>
     <x>211</x>
     <y>299</y>
     <width>61</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text" >
    <string>/</string>
   </property>
  </widget>
  <widget class="QPushButton" name="equlBtn" >
   <property name="geometry" >
    <rect>
     <x>212</x>
     <y>339</y>
     <width>61</width>
     <height>51</height>
    </rect>
   </property>
   <property name="text" >
    <string>=</string>
   </property>
  </widget>
  <widget class="QLabel" name="disLbl" >
   <property name="geometry" >
    <rect>
     <x>16</x>
     <y>16</y>
     <width>251</width>
     <height>41</height>
    </rect>
   </property>
   <property name="layoutDirection" >
    <enum>Qt::LeftToRight</enum>
   </property>
   <property name="frameShape" >
    <enum>QFrame::Panel</enum>
   </property>
   <property name="frameShadow" >
    <enum>QFrame::Sunken</enum>
   </property>
   <property name="lineWidth" >
    <number>3</number>
   </property>
   <property name="midLineWidth" >
    <number>0</number>
   </property>
   <property name="text" >
    <string/>
   </property>
   <property name="alignment" >
    <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

Ui_calculator.h

/********************************************************************************
** Form generated from reading ui file 'calculator.ui'
**
** Created: Wed Feb 26 13:31:54 2014
**      by: Qt User Interface Compiler version 4.2.1
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
********************************************************************************/

#ifndef UI_CALCULATOR_H
#define UI_CALCULATOR_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>

class Ui_calForm
{
public:
    QPushButton *_6Btn;
    QPushButton *_4Btn;
    QPushButton *_3Btn;
    QPushButton *_2Btn;
    QPushButton *_1Btn;
    QPushButton *_0Btn;
    QPushButton *dotBtn;
    QPushButton *_8Btn;
    QPushButton *_5Btn;
    QPushButton *_9Btn;
    QPushButton *_7Btn;
    QPushButton *backBtn;
    QPushButton *negBtn;
    QPushButton *plusBtn;
    QPushButton *sqrtBtn;
    QPushButton *modBtn;
    QPushButton *calBtn;
    QPushButton *minusBtn;
    QPushButton *multiBtn;
    QPushButton *divideBtn;
    QPushButton *equlBtn;
    QLabel *disLbl;

    void setupUi(QWidget *calForm)
    {
    calForm->setObjectName(QString::fromUtf8("calForm"));
    calForm->setWindowModality(Qt::NonModal);
    calForm->setWindowIcon(QIcon(QString::fromUtf8("image/calculator.PNG")));
    calForm->setAutoFillBackground(true);
    _6Btn = new QPushButton(calForm);
    _6Btn->setObjectName(QString::fromUtf8("_6Btn"));
    _6Btn->setGeometry(QRect(5, 252, 51, 27));
    _4Btn = new QPushButton(calForm);
    _4Btn->setObjectName(QString::fromUtf8("_4Btn"));
    _4Btn->setGeometry(QRect(142, 252, 51, 27));
    _3Btn = new QPushButton(calForm);
    _3Btn->setObjectName(QString::fromUtf8("_3Btn"));
    _3Btn->setGeometry(QRect(5, 303, 51, 27));
    _2Btn = new QPushButton(calForm);
    _2Btn->setObjectName(QString::fromUtf8("_2Btn"));
    _2Btn->setGeometry(QRect(72, 303, 51, 27));
    _1Btn = new QPushButton(calForm);
    _1Btn->setObjectName(QString::fromUtf8("_1Btn"));
    _1Btn->setGeometry(QRect(142, 303, 51, 27));
    _0Btn = new QPushButton(calForm);
    _0Btn->setObjectName(QString::fromUtf8("_0Btn"));
    _0Btn->setGeometry(QRect(5, 359, 51, 27));
    dotBtn = new QPushButton(calForm);
    dotBtn->setObjectName(QString::fromUtf8("dotBtn"));
    dotBtn->setGeometry(QRect(72, 359, 51, 27));
    _8Btn = new QPushButton(calForm);
    _8Btn->setObjectName(QString::fromUtf8("_8Btn"));
    _8Btn->setGeometry(QRect(72, 201, 51, 27));
    _5Btn = new QPushButton(calForm);
    _5Btn->setObjectName(QString::fromUtf8("_5Btn"));
    _5Btn->setGeometry(QRect(72, 252, 51, 27));
    _9Btn = new QPushButton(calForm);
    _9Btn->setObjectName(QString::fromUtf8("_9Btn"));
    _9Btn->setGeometry(QRect(5, 201, 51, 27));
    _7Btn = new QPushButton(calForm);
    _7Btn->setObjectName(QString::fromUtf8("_7Btn"));
    _7Btn->setGeometry(QRect(142, 201, 51, 27));
    backBtn = new QPushButton(calForm);
    backBtn->setObjectName(QString::fromUtf8("backBtn"));
    backBtn->setGeometry(QRect(209, 108, 61, 31));
    negBtn = new QPushButton(calForm);
    negBtn->setObjectName(QString::fromUtf8("negBtn"));
    negBtn->setGeometry(QRect(142, 359, 51, 27));
    plusBtn = new QPushButton(calForm);
    plusBtn->setObjectName(QString::fromUtf8("plusBtn"));
    plusBtn->setGeometry(QRect(210, 151, 61, 31));
    sqrtBtn = new QPushButton(calForm);
    sqrtBtn->setObjectName(QString::fromUtf8("sqrtBtn"));
    sqrtBtn->setGeometry(QRect(140, 151, 51, 31));
    modBtn = new QPushButton(calForm);
    modBtn->setObjectName(QString::fromUtf8("modBtn"));
    modBtn->setGeometry(QRect(70, 151, 51, 31));
    calBtn = new QPushButton(calForm);
    calBtn->setObjectName(QString::fromUtf8("calBtn"));
    calBtn->setGeometry(QRect(142, 108, 51, 31));
    minusBtn = new QPushButton(calForm);
    minusBtn->setObjectName(QString::fromUtf8("minusBtn"));
    minusBtn->setGeometry(QRect(211, 201, 61, 31));
    multiBtn = new QPushButton(calForm);
    multiBtn->setObjectName(QString::fromUtf8("multiBtn"));
    multiBtn->setGeometry(QRect(211, 248, 61, 31));
    divideBtn = new QPushButton(calForm);
    divideBtn->setObjectName(QString::fromUtf8("divideBtn"));
    divideBtn->setGeometry(QRect(211, 299, 61, 31));
    equlBtn = new QPushButton(calForm);
    equlBtn->setObjectName(QString::fromUtf8("equlBtn"));
    equlBtn->setGeometry(QRect(212, 339, 61, 51));
    disLbl = new QLabel(calForm);
    disLbl->setObjectName(QString::fromUtf8("disLbl"));
    disLbl->setGeometry(QRect(16, 16, 251, 41));
    disLbl->setLayoutDirection(Qt::LeftToRight);
    disLbl->setFrameShape(QFrame::Panel);
    disLbl->setFrameShadow(QFrame::Sunken);
    disLbl->setLineWidth(3);
    disLbl->setMidLineWidth(0);
    disLbl->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);

    retranslateUi(calForm);

    QSize size(282, 404);
    size = size.expandedTo(calForm->minimumSizeHint());
    calForm->resize(size);


    QMetaObject::connectSlotsByName(calForm);
    } // setupUi

    void retranslateUi(QWidget *calForm)
    {
    calForm->setWindowTitle(QApplication::translate("calForm", "\350\256\241\347\256\227\345\231\250", 0, QApplication::UnicodeUTF8));
    _6Btn->setText(QApplication::translate("calForm", "6", 0, QApplication::UnicodeUTF8));
    _4Btn->setText(QApplication::translate("calForm", "4", 0, QApplication::UnicodeUTF8));
    _3Btn->setText(QApplication::translate("calForm", "3", 0, QApplication::UnicodeUTF8));
    _2Btn->setText(QApplication::translate("calForm", "2", 0, QApplication::UnicodeUTF8));
    _1Btn->setText(QApplication::translate("calForm", "1", 0, QApplication::UnicodeUTF8));
    _0Btn->setText(QApplication::translate("calForm", "0", 0, QApplication::UnicodeUTF8));
    dotBtn->setText(QApplication::translate("calForm", ".", 0, QApplication::UnicodeUTF8));
    _8Btn->setText(QApplication::translate("calForm", "8", 0, QApplication::UnicodeUTF8));
    _5Btn->setText(QApplication::translate("calForm", "5", 0, QApplication::UnicodeUTF8));
    _9Btn->setText(QApplication::translate("calForm", "9", 0, QApplication::UnicodeUTF8));
    _7Btn->setText(QApplication::translate("calForm", "7", 0, QApplication::UnicodeUTF8));
    backBtn->setText(QApplication::translate("calForm", "<-", 0, QApplication::UnicodeUTF8));
    negBtn->setText(QApplication::translate("calForm", "-/+", 0, QApplication::UnicodeUTF8));
    plusBtn->setText(QApplication::translate("calForm", "+", 0, QApplication::UnicodeUTF8));
    sqrtBtn->setText(QApplication::translate("calForm", "Sqrt", 0, QApplication::UnicodeUTF8));
    modBtn->setText(QApplication::translate("calForm", "Mod", 0, QApplication::UnicodeUTF8));
    calBtn->setText(QApplication::translate("calForm", "C", 0, QApplication::UnicodeUTF8));
    minusBtn->setText(QApplication::translate("calForm", "-", 0, QApplication::UnicodeUTF8));
    multiBtn->setText(QApplication::translate("calForm", "*", 0, QApplication::UnicodeUTF8));
    divideBtn->setText(QApplication::translate("calForm", "/", 0, QApplication::UnicodeUTF8));
    equlBtn->setText(QApplication::translate("calForm", "=", 0, QApplication::UnicodeUTF8));
    disLbl->setText(QString());
    Q_UNUSED(calForm);
    } // retranslateUi

};

namespace Ui {
    class calForm: public Ui_calForm {};
} // namespace Ui

#endif // UI_CALCULATOR_H


calculator.h

#ifndef CALCULATOR_H
#define CALCULATOR_H
//#include <QWidget>
#include <QDialog>
#include <QString>
#include <QStack>
#include "Ui_calculator.h"

class CalDialog : public QDialog ,public Ui_calForm
{
Q_OBJECT
public:
	CalDialog();
	QString showedit;	//顯示
	int op;				//當前操作
	int lastop;			//上次操作
	bool dot;			//是否輸入小數點
	bool first;			//是否是第一次輸入
	bool digit;			//是否輸入數字
	bool on_num1;		//操作數1是否有效
	bool on_num2;		//操作數2是否有效
	double num1;		//操作數1
	double num2;		//操作數2
    int num3;			//操作數1取整,在求模時用到
    int num4;			//操作數2取整
	void trans( int num );//傳入數字並顯示
	void test();		  //測試是否第一次運算
private slots:
	/* 傳入數字鍵盤上相應的數字 */
	void Print0();
	void Print1();
	void Print2();
	void Print3();
	void Print4();
	void Print5();
	void Print6();
	void Print7();
	void Print8();
	void Print9();
	/*加,減,乘,除,取模,開方,等於,小數點,負數*/
	void Plus();
	void Minus();
	void Mul();
	void Div();
	void Clr();
	void Equal();
	void Backspace();
	void Dot();
    void Neg();
    void Mod();
    void Sqrt();
};
#endif

calculator.cpp

#include <QtGui>
#include "calculator.h"
#include <math.h>

CalDialog::CalDialog() :QDialog()
{
	/*參數初始化*/
	setupUi(this);
	showedit = "0.";
	op = -1;
	dot = false;
	first = true;
	lastop = -1;
	on_num1 = false;
	on_num2 = false;
	digit = false;
	/* 綁定信號和槽函數 */
	connect( _0Btn, SIGNAL(clicked() ), this, SLOT(Print0()) );
	connect( _1Btn, SIGNAL(clicked() ), this, SLOT(Print1()) );
	connect( _2Btn, SIGNAL(clicked() ), this, SLOT(Print2()) );
	connect( _3Btn, SIGNAL(clicked() ), this, SLOT(Print3()) );
	connect( _4Btn, SIGNAL(clicked() ), this, SLOT(Print4()) );
	connect( _5Btn, SIGNAL(clicked() ), this, SLOT(Print5()) );
	connect( _6Btn, SIGNAL(clicked() ), this, SLOT(Print6()) );
	connect( _7Btn, SIGNAL(clicked() ), this, SLOT(Print7()) );
	connect( _8Btn, SIGNAL(clicked() ), this, SLOT(Print8()) );
	connect( _9Btn, SIGNAL(clicked() ), this, SLOT(Print9()) );


	connect( plusBtn, SIGNAL(clicked() ), this, SLOT(Plus()) );
	connect( minusBtn, SIGNAL(clicked() ), this, SLOT(Minus()) );
	connect( multiBtn, SIGNAL(clicked() ), this, SLOT(Mul()) );
	connect( divideBtn, SIGNAL(clicked() ), this, SLOT(Div()) );

    connect( sqrtBtn, SIGNAL(clicked() ), this, SLOT(Sqrt() ) );
    connect( modBtn, SIGNAL(clicked() ), this, SLOT(Mod() ));

	connect( equlBtn, SIGNAL(clicked() ), this, SLOT(Equal()) );

	connect( calBtn, SIGNAL(clicked() ), this, SLOT(Clr()) );

	connect( backBtn, SIGNAL(clicked() ), this, SLOT(Backspace()));

	connect( dotBtn, SIGNAL(clicked() ), this, SLOT(Dot()) );
    connect( negBtn, SIGNAL(clicked() ), this, SLOT(Neg() ) );

}

/*取負數操作:先輸入數字再點擊符號鍵*/
void CalDialog::Neg()
{
    QString temp;
    temp = disLbl->text();
    if(  temp != "0." && temp != "error" && temp[0] != '-' )
    {
        temp = "-" + temp;
        disLbl->setText( temp );
    }
}
void CalDialog::trans( int num )
{
	if( showedit == "0." )
	{
		if( dot == false )
			showedit = QString::number(num)+".";	//第一個輸入非小數點,顯示:"數字."
		else
			showedit.append( QString::number(num) );//第一個輸入是小數點,顯示:"0.數字"
	}
	else
	{
		if( showedit.length() < 16 )
		{
			if( dot == false )						//當前非小數數字插入到小數點前
				showedit.insert( showedit.length()-1, QString::number(num) );
			else									//當前是小數數字插入到小數點後
				showedit.insert( showedit.length(), QString::number(num) );
		}
	}
	disLbl->setText( showedit );
}

void CalDialog::Sqrt()
{
    //int num;
    double num;
    QString temp;
    temp = disLbl->text();
    num = temp.toDouble();
    num=sqrt(num);
    disLbl->setText( QString::number(num) );
}
void CalDialog::Mod()
{
    op = '%';
    test();
    dot = false;
    showedit = "0.";
}

void CalDialog::Plus()
{
    op = '+';
    test();
    dot = false;
    showedit = "0.";
}
void CalDialog::Minus()
{
    op = '-';
    test();
    dot = false;
    showedit = "0.";
}
void CalDialog::Mul()
{
    op = '*';
    test();
    dot = false;
    showedit = "0.";
}
void CalDialog::Div()
{
    op = '/';
    test();
    dot = false;
    showedit = "0.";
}
void CalDialog::Equal( )
{
    QString temp;
    if( first  && lastop == -1 ) return;           //如果只輸入了一個數字直接按等號
    if( digit )                                    //如果第二次輸入是一個數 如 1+2  而不是 輸入 1 +  +
    {                                              //digit  設在槽函數中用來測試按鍵是否是數字
        temp = disLbl->text();
        num2 = temp.toDouble();
        num4 = (int)num2;
		on_num2 = true;
	}
	if( op != -1 )
	{
		if( lastop == '+' && on_num1 && on_num2 )
		{
			num1 += num2;
			if( QString::number(num1).contains(".") )
				disLbl->setText( QString::number(num1)  );
			else
				disLbl->setText( QString::number(num1) + '.' );
			on_num2 = false;
		}
		if( lastop == '-' && on_num1 && on_num2 )
		{
			num1 -= num2;
			if( QString::number(num1).contains(".") )
				disLbl->setText( QString::number(num1)  );
			else
				disLbl->setText( QString::number(num1) + '.' );
			on_num2 = false;
		}
		if( lastop == '*' && on_num1 && on_num2 )
		{
			num1 *= num2;
			if( QString::number(num1).contains(".") )
				disLbl->setText( QString::number(num1)  );
			else
				disLbl->setText( QString::number(num1) + '.' );
			on_num2 = false;
		}
		if( lastop == '/' && on_num1 && on_num2  )
		{
			if( num2 == 0 )
				disLbl->setText( "error" );
			else
			{
                num1 /= num2;
				if( QString::number(num1).contains(".") )
					disLbl->setText( QString::number(num1)  );
				else
					disLbl->setText( QString::number(num1) + '.' );
				on_num2 = false;
			}
        }
       if( lastop == '%' && on_num1 && on_num2   )
       {
           if( num2 == 0 ||  temp.at( temp.length()-1 ) != '.'  )
                    disLbl->setText( "error" );
            else
            {
                num3 %= num4;
                num1 = num3;
                if( QString::number(num1).contains(".") )
                    disLbl->setText( QString::number(num1)  );
                else
                    disLbl->setText( QString::number(num1) + '.' );
                on_num2 = false;
            }
		}
        first = true;
		dot = false;
		showedit = "0.";
	}
}
void CalDialog::test()
{
    if( !first )                    //如果不是第一次使用符號,如:1+ 1 + ,此時應該調用等號先算出2再進行加
        Equal();
    else                            //如果是第一次用符號1+ ,此時把1存進第一個數
	{
		if( digit  )
		{
            //lastop = op;
			first = false;
            showedit= disLbl->text();
            num1 = showedit.toDouble();
            num3 = (int)num1;
			on_num1 = true;
		}
     }
    lastop = op;
	digit = false;
}
void CalDialog::Backspace() //刪除數字
{
	showedit = disLbl->text();
	if( showedit != "0." )
	{
       if( showedit.length() == 2 || (showedit.length()==3 && showedit.at(0) == '-' ) )
			Clr();
		else
		{
			if( showedit.at(showedit.length()-1) != '.' )
				showedit.remove( showedit.length()-1, 1);
			else
				showedit.remove( showedit.length()-2, 1);
		}
	}
	disLbl->setText( showedit );
}

void CalDialog::Clr()  //清零
{
	showedit = "0.";
	disLbl->setText( showedit );
	num1 = 0;
	num2 = 0;
	first = true;
	dot = false;
}

void CalDialog::Print0()
{
	digit = true;
	trans( 0 );
}

void CalDialog::Print1()
{
	digit = true;
	trans( 1 );
}

void CalDialog::Print2()
{
	digit = true;
	trans( 2 );
}

void CalDialog::Print3()
{
	digit = true;
	trans( 3 );
}

void CalDialog::Print4()
{
	digit = true;
	trans( 4 );
}

void CalDialog::Print5()
{
	digit = true;
	trans( 5 );
}

void CalDialog::Print6()
{
	digit = true;
	trans( 6 );
}

void CalDialog::Print7()
{
	digit = true;
	trans( 7 );
}

void CalDialog::Print8()
{
	digit = true;
	trans( 8 );
}

void CalDialog::Print9()
{
	digit = true;
	trans( 9 );
}

void CalDialog::Dot()
{
	if( dot == false )
        dot = true;
}


main.cpp
#include <QApplication>
#include "calculator.h"
#include "Ui_calculator.h"
int main( int argc, char * argv[] )
{
	QApplication app( argc, argv );
    //app.setWindowIcon(QIcon(":image/calculator.PNG"));
	CalDialog *cal = new CalDialog;
	cal->show();
	return app.exec();
}

calculator.pro

######################################################################
# Automatically generated by qmake (2.01a) ?? ?? 25 18:51:49 2014
######################################################################

TEMPLATE = app
TARGET = calculator 
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += calculator.h
FORMS += calculator.ui
SOURCES += calculator.cpp main.cpp

本來想加入圖標的,最終沒有成功,有知道怎麼在Linux下給Qt應用加圖標的大牛歡迎留言指教!

源代碼下載敬請移步:http://download.csdn.net/detail/u011493704/6964215

或點擊代碼下載鏈接


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章