本文主要是介绍Qt根据单价计算总价与进制转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.相关说明
二进制、十进制、十六进制间的相互转换
2.界面绘制
3.相关主要代码
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}// 计算总价
void Widget::on_btnTotal_clicked()
{bool ok = false;int num = ui->editNum->text().toInt(&ok, 10);if(!ok){qDebug("read num failed \n");return;}float price = ui->editPrice->text().toFloat(&ok);if(!ok){qDebug("read price failed \n");return;}float total = price * num;QString str = QString::number(total, 'f', 2);ui->editTotal->setText(str);
}// qDebug测试
void Widget::on_btnDebug_clicked()
{qDebug("PI = %f", 3.14159265);QString str = QString::number(3.14159265, 'f', 10);qDebug("PI = %s", str.toLocal8Bit().data());
}// DEC --> 其他进制
void Widget::on_btnDecToOther_clicked()
{bool ok = false;int val = ui->editDec->text().toInt(&ok, 10);if(!ok){qDebug("DEC --> other failed \n");return;}QString strHex = QString::number(val, 16);ui->editHex->setText(strHex);QString strBin = QString::number(val, 2);ui->editBin->setText(strBin);
}// BIN --> 其他进制
void Widget::on_btnBinToOther_clicked()
{bool ok = false;int val = ui->editBin->text().toInt(&ok, 2);if(!ok){qDebug("BIN --> other failed \n");return;}QString strHex = QString::number(val, 16);ui->editHex->setText(strHex);QString strDec = QString::number(val, 10);ui->editDec->setText(strDec);
}// HEX --> 其他进制
void Widget::on_btnHexToOther_clicked()
{bool ok = false;int val = ui->editHex->text().toInt(&ok, 16);if(!ok){qDebug("HEX --> other failed \n");return;}QString strBin = QString::number(val, 2);ui->editBin->setText(strBin);QString strDec = QString::number(val, 10);ui->editDec->setText(strDec);
}
这篇关于Qt根据单价计算总价与进制转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!