这几天在学习QT,按照官方Nikia给的英文课件在学
痛苦程度不亚于被强奸
练习是让我们补全Calculator类中信号与槽应用的代码
瞎搞了3个小时,终于补全
也像被强奸一样,快结束的时候是有快感的
一、功能
记录结果的简单的计算器,带清空输入和清空结果功能。
二、实现
界面和计算过程分开,各位一个类,一个为Calculator类,一个为Ui::CalculatorInterface,用CalculatorInterface分别调用这两个类,然后进行交互。
三、源代码
//Calculator类的方法实现 #include "calculator.h" #include <qmessagebox.h> Calculator::Calculator(QObject *parent) : QObject(parent), m_lastResult(0), m_currentNumber(0), m_isAdding(true) { } void Calculator::numEntered(int num) { m_currentNumber = m_currentNumber*10 + num; //emit就是在放贱 emit displayChanged(QString::number(m_currentNumber)); } void Calculator::clear() { m_currentNumber = 0; //emit就是在放贱 emit displayChanged(QString::number(m_currentNumber)); } void Calculator::allClear() { m_currentNumber = 0; m_lastResult = 0; //emit就是在放贱 emit displayChanged(QString::number(m_currentNumber)); } void Calculator::additionMode() { calculate(); m_isAdding = true; } void Calculator::subtractionMode() { calculate(); m_isAdding = false; } void Calculator::calculate() { m_lastResult += m_isAdding?m_currentNumber:-m_currentNumber; m_currentNumber = 0; //emit就是在放贱 emit displayChanged(QString::number(m_lastResult)); }
//CalculatorInterface的方法实现 #include "calculatorinterface.h" #include "ui_calculatorinterface.h" #include <QSignalMapper> #include "calculator.h" CalculatorInterface::CalculatorInterface(QWidget *parent) : QWidget(parent), ui(new Ui::CalculatorInterface), m_calculator(new Calculator(this)) { ui->setupUi(this); //信号映射容器 QSignalMapper *m = new QSignalMapper(this); //ui->buttonZero的clicked信号映射到m的0 connect(ui->buttonZero,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonZero,0); connect(ui->buttonOne,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonOne,1); connect(ui->buttonTwo,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonTwo,2); connect(ui->buttonThree,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonThree,3); connect(ui->buttonFour,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonFour,4); connect(ui->buttonFive,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonFive,5); connect(ui->buttonSix,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonSix,6); connect(ui->buttonSeven,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonSeven,7); connect(ui->buttonEight,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonEight,8); connect(ui->buttonNine,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonNine,9); connect(ui->buttonAdd,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonAdd,10); connect(ui->buttonAllClear,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonAllClear,11); connect(ui->buttonCalculate,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonCalculate,12); connect(ui->buttonClear,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonClear,13); connect(ui->buttonSubtract,SIGNAL(clicked()),m,SLOT(map())); m->setMapping(ui->buttonSubtract,14); //m_calculator类的dispalayChanged与ui的label绑定,一旦m_...有变化,ui的label就更新text connect(m_calculator,SIGNAL(displayChanged(QString)),ui->entryLabel,SLOT(setText(QString))); //当m的map接受到信号的时候mmaped发射,并与keyPressed槽绑定,keypressed调用m_calculator相应的方法 connect(m,SIGNAL(mapped(int)),this,SLOT(keyPressed(int))); //初始化 m_calculator->allClear(); } CalculatorInterface::~CalculatorInterface() { delete ui; } void CalculatorInterface::keyPressed(int key) { if (key <= 9) m_calculator->numEntered(key); else if (key == 10) m_calculator->additionMode(); else if (key == 11) m_calculator->allClear(); else if (key == 12) m_calculator->calculate(); else if (key == 13) m_calculator->clear(); else if (key == 14) m_calculator->subtractionMode(); } void CalculatorInterface::changeEvent(QEvent *e) { QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } }
//main #include <QtGui/QApplication> #include "calculatorinterface.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); CalculatorInterface w; w.show(); return a.exec(); }
四、总结
官方的这种实现十分精巧,利用CalculatorInterface来连接运算类和界面类并进行交互,逻辑清晰,代码简洁。
这种实现方式很值得学习。
本文ZeroClock原创,但可以转载,因为我们是兄弟。