对于Qt的学习我以为应该以应用实例为主体,不断地扩充自己的Qt知识,在实践中不断地学习;
编程环境:
系统:win7 32位
软件:vs2013 + Qt5.8.0
首先要干的是通过Designer,画出视图来主要是两部分文本编辑框(QText)和按键(QPushButton);
然后编写相关代码;
完成这个小应用,加深了对Qt中Style Sheets的了解,可以通过setStyleSheet(…)是我们要设计的Widget更加的美观;
例如:
setStyleSheet("QTextEdit{background-color: rgb(255, 255, 255);font-family:'Microsoft YaHei';font-size:35px;font-weight:bold;}");
设置QTextEdit中的白色背景色,字体微软雅黑,字体大小35个像素,加粗;
QButtonGroup 是一个按键组,当我们需要用到多个功能相似的按键时,使用该类将会大大地减少我们的工作量;
是吃Qt这碗饭的,但是刚接触4个月需要学习的还有很多啊;加油~~~
相关代码如下:
myCalc.h
#ifndef MYCALC_H
#define MYCALC_H
#include <QtWidgets/QDialog>
#include "ui_mycalc.h"
#include <QButtonGroup>
#include <QDebug>
#include <QStringList>
class myCalc : public QDialog
{
Q_OBJECT
public:
myCalc(QWidget *parent = 0);
~myCalc();
QString calculate(int id);
public slots:
void onDigitGroupClicked(int id);
void onFunctionGroupClicked(int id);
void onResultGroupClicked(int id);
private:
Ui::myCalcClass *ui;
QButtonGroup *digitGroup;
QButtonGroup *functionGroup;
QButtonGroup *resultGroup;
QString m_expression;
QString m_curValue;
int m_operatorId;
};
#endif // MYCALC_H
myCalc.cpp
#include "mycalc.h"
myCalc::myCalc(QWidget *parent)
: QDialog(parent)
, ui(new Ui::myCalcClass)
, m_expression("0")
, m_curValue("0")
, m_operatorId(0)
{
ui->setupUi(this);
digitGroup = new QButtonGroup;
functionGroup = new QButtonGroup;
resultGroup = new QButtonGroup;
digitGroup->addButton(ui->pushButton_0, 0);
digitGroup->addButton(ui->pushButton_1, 1);
digitGroup->addButton(ui->pushButton_2, 2);
digitGroup->addButton(ui->pushButton_3, 3);
digitGroup->addButton(ui->pushButton_4, 4);
digitGroup->addButton(ui->pushButton_5, 5);
digitGroup->addButton(ui->pushButton_6, 6);
digitGroup->addButton(ui->pushButton_7, 7);
digitGroup->addButton(ui->pushButton_8, 8);
digitGroup->addButton(ui->pushButton_9, 9);
digitGroup->addButton(ui->pushButton_10, 10);
functionGroup->addButton(ui->pushButton_plus, 21);
functionGroup->addButton(ui->pushButton_sub, 22);
functionGroup->addButton(ui->pushButton_mult, 23);
functionGroup->addButton(ui->pushButton_excpt, 24);
resultGroup->addButton(ui->pushButton_equal, 31);
resultGroup->addButton(ui->pushButton_reciprocial, 32);
resultGroup->addButton(ui->pushButton_back, 33);
resultGroup->addButton(ui->pushButton_zero, 34);
resultGroup->addButton(ui->pushButton_percent, 35);
resultGroup->addButton(ui->pushButton_PlusAndSub, 36);
connect(digitGroup, SIGNAL(buttonClicked(int)), this, SLOT(onDigitGroupClicked(int)));
connect(functionGroup, SIGNAL(buttonClicked(int)), this, SLOT(onFunctionGroupClicked(int)));
connect(resultGroup, SIGNAL(buttonClicked(int)), this, SLOT(onResultGroupClicked(int)));
ui->textEdit->setReadOnly(true);
setStyleSheet("QTextEdit{background-color: rgb(255, 255, 255);font-family:'Microsoft YaHei';font-size:35px;font-weight:bold;}");
ui->textEdit->setText(m_expression + "\r\n" + m_curValue);
}
myCalc::~myCalc()
{
}
#define COMMON_STYLE_SHEET "background-color: rgb(239,246,255);color: rgb(0, 0, 0);border-width: 1px; font-family:'Microsoft YaHei';font-size:15px;"
void myCalc::onDigitGroupClicked(int id)
{
if (m_curValue == "0" || m_expression == "0")
{
m_curValue.clear();
m_expression.clear();
}
if (id >= 0 && id <= 10)
{
m_curValue += digitGroup->button(id)->text();
m_expression += digitGroup->button(id)->text();
}
qDebug() << "onDigitGroupClicked success" << m_expression;
ui->textEdit->setText(m_expression + "\r\n" + m_curValue);
}
void myCalc::onFunctionGroupClicked(int id)
{
m_expression += functionGroup->button(id)->text();
ui->textEdit->setText(m_expression + "\r\n" + m_curValue);
m_operatorId = id;
m_curValue.clear();
}
void myCalc::onResultGroupClicked(int id)
{
QString result;
switch (id)
{
case 31:
result = calculate(id);
m_expression = result;
m_curValue = result;
break;
case 32:
if (m_curValue == 0)
{
result = QString(tr("ERROR"));
break;
}
result = QString("%1").arg(1 / m_curValue.toFloat());
m_expression = result;
m_curValue = result;
break;
case 33:
m_curValue.chop(1);
m_expression.chop(1);
if (m_curValue.isEmpty())
m_curValue = "0";
ui->textEdit->setText(m_expression + "\r\n" + m_curValue);
return;
case 34:
m_curValue = "0";
m_expression.clear();
ui->textEdit->setText("\r\n" + m_curValue);
return;
case 35:
result = "0";
m_expression = "0";
m_curValue = "0";
break;
case 36:
result = QString("%1").arg(0 - m_curValue.toFloat());
m_expression = result;
m_curValue = result;
break;
default:
break;
}
ui->textEdit->setText("\r\n" + result);
qDebug() << m_expression;
m_operatorId = 0;
}
QString myCalc::calculate(int id)
{
if (m_operatorId == 0)
return m_expression;
QString operatorSymbol = functionGroup->button(m_operatorId)->text();
QStringList str = m_expression.split(operatorSymbol);
float frist = str.at(0).toFloat();
float scend = str.at(1).toFloat();
qDebug() << frist << " " << operatorSymbol << " " << scend;
float result;
switch (m_operatorId)
{
case 21:
result = frist + scend;
break;
case 22:
result = frist - scend;
break;
case 23:
result = frist * scend;
break;
case 24:
if (scend == 0)
return QString("ERROR");
result = frist / scend;
break;
default:
break;
}
return QString("%1").arg(result);
}