QT 进制转换小工具 二进制/十进制/十六进制/浮点数转换

您所在的位置:网站首页 f0转为二进制 QT 进制转换小工具 二进制/十进制/十六进制/浮点数转换

QT 进制转换小工具 二进制/十进制/十六进制/浮点数转换

2024-02-01 08:06| 来源: 网络整理| 查看: 265

野生小菜鸟一只,程序效果如下,希望大家多多支持。

1. 按table切换LineEdit;

2. 输入要计算的数据后,按回车执行,计算后的数据会显示在同一行的另一个控件中。

3. LIneEdit内加入了正则表达式使之只能输入特定的数。

format.h

#ifndef FORMAT_H #define FORMAT_H #include namespace Ui { class Format; } class Format : public QDialog { Q_OBJECT public: explicit Format(QWidget *parent = 0); ~Format(); private: Ui::Format *ui; private slots: void twoTten(); void twoThex(); void tenTtwo(); void tenThex(); void hexTtwo(); void hexTten(); void floatThex(); void hexTfloat(); private: QString binToDec(QString strBin); QString decTobin(QString strDec); QString decToHex(QString strDec); QString hexToDec(QString strHex); }; #endif // FORMAT_H

format.cpp

#include "format.h" #include "ui_format.h" #include Format::Format(QWidget *parent) : QDialog(parent), ui(new Ui::Format) { ui->setupUi(this); this->setAutoFillBackground(true); this->setPalette(QPalette(QColor(162,200,169))); QRegExp regBinary("[0-1]*"); QValidator *validator = new QRegExpValidator(regBinary,this); ui->two_ten1->setValidator(validator); ui->two_hex1->setValidator(validator); QRegExp regDecimal("[0-9]*"); QValidator *validator2 = new QRegExpValidator(regDecimal,this); ui->ten_hex1->setValidator(validator2); ui->ten_two1->setValidator(validator2); QRegExp regHexadecimal("[a-fA-F0-9]*"); QValidator *validator3 = new QRegExpValidator(regHexadecimal,this); ui->hex_ten1->setValidator(validator3); ui->hex_two1->setValidator(validator3); connect(ui->two_ten1,SIGNAL(returnPressed()),this,SLOT(twoTten())); connect(ui->two_hex1,SIGNAL(returnPressed()),this,SLOT(twoThex())); connect(ui->ten_hex1,SIGNAL(returnPressed()),this,SLOT(tenThex())); connect(ui->ten_two1,SIGNAL(returnPressed()),this,SLOT(tenTtwo())); connect(ui->hex_ten1,SIGNAL(returnPressed()),this,SLOT(hexTten())); connect(ui->hex_two1,SIGNAL(returnPressed()),this,SLOT(hexTtwo())); connect(ui->float_hex1,SIGNAL(returnPressed()),this,SLOT(floatThex())); connect(ui->hex_float1,SIGNAL(returnPressed()),this,SLOT(hexTfloat())); } Format::~Format() { delete ui; } void Format::twoTten(){ //二进制转十进制 QString binary = ui->two_ten1->text(); QString decimal = binToDec(binary); ui->ten_two1->setText(decimal); } void Format::twoThex(){ //二进制转十六进制 QString binary = ui->two_hex1->text(); QString hexadecimal = decToHex(binToDec(binary)); ui->hex_two1->setText(hexadecimal); } void Format::tenThex(){ //十进制转十六进制 QString decimal = ui->ten_hex1->text(); QString hexadecimal = decToHex(decimal); ui->hex_ten1->setText(hexadecimal); } void Format::tenTtwo(){ //十进制转二进制 QString decimal = ui->ten_two1->text(); QString binary = decTobin(decimal); ui->two_ten1->setText(binary); } void Format::hexTten(){ //十六进制转十进制 QString hexadecimal = ui->hex_ten1->text(); QString decimal = hexToDec(hexadecimal); ui->ten_hex1->setText(decimal); } void Format::hexTtwo(){ //十六进制转二进制 QString hexadecimal = ui->hex_two1->text(); QString binary = decTobin(hexToDec(hexadecimal)); ui->two_hex1->setText(binary); } void Format::floatThex(){ QString strFloat = ui->float_hex1->text(); float f = strFloat.toFloat(); int i = *((int *)&f); QString float2 = QString("%1").arg(i,8,16,QLatin1Char('0')); QString step =float2; float2 = float2.right(4)+step.left(4); ui->hex_float1->setText(float2); } void Format::hexTfloat(){ QString strHex = ui->hex_float1->text(); QString step = strHex; strHex = strHex.right(4)+step.left(4); int c = hexToDec(strHex).toInt(); float d = *(float*)&c; QString radiation = QString("%1").arg(d); ui->float_hex1->setText(radiation); }

xijei.cpp

#include "format.h" #include QString Format::binToDec(QString strBin){ //二进制转十进制 QString decimal; int nDec = 0,nLen; int i,j,k; nLen = strBin.length(); for(i=0;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3