当前位置:网站首页>Qt(六)数值与字符串转换
Qt(六)数值与字符串转换
2022-07-15 17:58:00 【51CTO】
文章目录
一、UI设计
新建项目,主要选项如下图:一个QWidget,带ui文件,并初步Ui设计
拖动组件:QLable,QPushButton,QlineEdit。

详情ui文件
<?xml
version="1.0" encoding="UTF-8"?>
<
ui
version=
"4.0"
>
<
class
>Widget
</
class
>
<
widget
class=
"QWidget"
name=
"Widget"
>
<
property
name=
"geometry"
>
<
rect
>
<
x
>0
</
x
>
<
y
>0
</
y
>
<
width
>511
</
width
>
<
height
>203
</
height
>
</
rect
>
</
property
>
<
property
name=
"windowTitle"
>
<
string
>Widget
</
string
>
</
property
>
<
layout
class=
"QGridLayout"
name=
"gridLayout_3"
>
<
item
row=
"0"
column=
"0"
>
<
layout
class=
"QGridLayout"
name=
"gridLayout"
>
<
item
row=
"0"
column=
"0"
>
<
widget
class=
"QLabel"
name=
"label"
>
<
property
name=
"text"
>
<
string
>数量:
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"0"
column=
"1"
>
<
widget
class=
"QLineEdit"
name=
"lineEditNum"
>
<
property
name=
"text"
>
<
string
>12
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"0"
column=
"2"
rowspan=
"2"
>
<
spacer
name=
"horizontalSpacer"
>
<
property
name=
"orientation"
>
<
enum
>Qt::Horizontal
</
enum
>
</
property
>
<
property
name=
"sizeHint"
stdset=
"0"
>
<
size
>
<
width
>17
</
width
>
<
height
>20
</
height
>
</
size
>
</
property
>
</
spacer
>
</
item
>
<
item
row=
"0"
column=
"3"
>
<
widget
class=
"QLabel"
name=
"label_2"
>
<
property
name=
"text"
>
<
string
>单价:
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"0"
column=
"4"
>
<
widget
class=
"QLineEdit"
name=
"lineEditPrice"
>
<
property
name=
"text"
>
<
string
>5.0
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"1"
column=
"1"
>
<
widget
class=
"QPushButton"
name=
"pushButton"
>
<
property
name=
"text"
>
<
string
>计算
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"1"
column=
"3"
>
<
widget
class=
"QLabel"
name=
"label_3"
>
<
property
name=
"text"
>
<
string
>总价:
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"1"
column=
"4"
>
<
widget
class=
"QLineEdit"
name=
"lineEditTotal"
/>
</
item
>
</
layout
>
</
item
>
<
item
row=
"1"
column=
"0"
>
<
spacer
name=
"verticalSpacer"
>
<
property
name=
"orientation"
>
<
enum
>Qt::Vertical
</
enum
>
</
property
>
<
property
name=
"sizeHint"
stdset=
"0"
>
<
size
>
<
width
>20
</
width
>
<
height
>9
</
height
>
</
size
>
</
property
>
</
spacer
>
</
item
>
<
item
row=
"2"
column=
"0"
>
<
layout
class=
"QGridLayout"
name=
"gridLayout_2"
>
<
item
row=
"0"
column=
"1"
>
<
widget
class=
"QLineEdit"
name=
"lineEditTen"
>
<
property
name=
"text"
>
<
string
>15
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"2"
column=
"2"
>
<
widget
class=
"QPushButton"
name=
"pushButton_4"
>
<
property
name=
"text"
>
<
string
>转换成其他进制
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"0"
column=
"2"
>
<
widget
class=
"QPushButton"
name=
"pushButton_2"
>
<
property
name=
"text"
>
<
string
>转换成其他进制
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"1"
column=
"0"
>
<
widget
class=
"QLabel"
name=
"label_5"
>
<
property
name=
"text"
>
<
string
>二进制:
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"2"
column=
"1"
>
<
widget
class=
"QLineEdit"
name=
"lineEditHex"
/>
</
item
>
<
item
row=
"2"
column=
"0"
>
<
widget
class=
"QLabel"
name=
"label_6"
>
<
property
name=
"text"
>
<
string
>十六进制:
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"1"
column=
"2"
>
<
widget
class=
"QPushButton"
name=
"pushButton_3"
>
<
property
name=
"text"
>
<
string
>转换成其他进制
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"1"
column=
"1"
>
<
widget
class=
"QLineEdit"
name=
"lineEditSec"
/>
</
item
>
<
item
row=
"0"
column=
"0"
>
<
widget
class=
"QLabel"
name=
"label_4"
>
<
property
name=
"text"
>
<
string
>十进制:
</
string
>
</
property
>
</
widget
>
</
item
>
</
layout
>
</
item
>
</
layout
>
</
widget
>
<
resources
/>
<
connections
/>
</
ui
>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
二、代码开发
代码使用“转到槽“生成代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace
Ui {
class
Widget; }
QT_END_NAMESPACE
class
Widget :
public
QWidget
{
Q_OBJECT
public:
Widget(
QWidget
*
parent
=
nullptr);
~Widget();
private
slots:
void
on_pushButton_clicked();
void
on_pushButton_2_clicked();
void
on_pushButton_3_clicked();
private:
Ui::Widget
*
ui;
};
#endif
// WIDGET_H
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
widget.cpp
#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_pushButton_clicked()
{
QString
str
=
ui
->
lineEditNum
->
text();
int
num
=
str.
toInt();
str
=
ui
->
lineEditPrice
->
text();
float
price
=
str.
toFloat();
float
total
=
num
*
price;
str
=
str.
setNum(
total,
'f',
2);
ui
->
lineEditTotal
->
setText(
str);
}
void
Widget::on_pushButton_2_clicked()
{
QString
str
=
ui
->
lineEditTen
->
text();
int
value
=
str.
toInt();
str
=
str.
setNum(
value,
2);
ui
->
lineEditSec
->
setText(
str);
str
=
str.
setNum(
value,
16);
ui
->
lineEditHex
->
setText(
str.
toUpper());
}
void
Widget::on_pushButton_3_clicked()
{
QString
str
=
ui
->
lineEditSec
->
text();
bool
ok;
int
value
=
str.
toInt(
&
ok,
2);
// 以二进制方式读入
ui
->
lineEditTen
->
setText(
QString::number(
value));
ui
->
lineEditHex
->
setText(
QString::number(
value,
16).
toUpper());
}
void
Widget::on_pushButton_4_clicked()
{
QString
str
=
ui
->
lineEditHex
->
text();
bool
ok;
int
value
=
str.
toInt(
&
ok,
16);
// 以十六进制方式读入
ui
->
lineEditTen
->
setText(
QString::number(
value));
ui
->
lineEditSec
->
setText(
QString::number(
value,
2));
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
三、演示效果
这里没有做容错处理,在输入字符时特别是:二进制只能是:01,十六进制只能是:0-9A-F。

边栏推荐
- Software architecture and design (VIII) -- distributed architecture
- Software architecture and design (VII) -- interactive architecture
- MYSQL和 ORACLE 的常见区别(二)
- 对接企业微信,客户关系管理也可以很简单!
- 蓝鲸配置框架
- Li Mu hands on deep learning V2 target detection SSD
- 每日一题·873.最长的斐波那契子序列的长度.记忆化搜索
- Lifecycle: the foundation of lifecycle aware components - jetpack series (1)
- 394. String decoding · stack
- GET 请求和 POST 请求的区别和使用
猜你喜欢

CTSI 基金会储备透明度报告——2022 年 7 月

GeoServer complete tutorial

C语言·前缀树实现

Des dizaines de milliards de données compressées à 600 go, tdengine est installé sur la plateforme mobile d'énergie de GCL

Torch in pytoch numel(),torch. shape,torch. Size () and torch Reshape() function parsing

SAP ABAP 系统进行数据库表查询的几种常用方法的试读版

How to set the allure test report

Broadcast mechanism in pytoch

338.比特位计数·动态规划

Openpyxl drawing pie chart
随机推荐
Software architecture and design (IV) -- data flow architecture
Meituan side: why does thread crash not cause JVM crash?
數百億數據壓縮至 600GB,TDengine 落地協鑫能科移動能源平臺
每日一题·1217.玩筹码·贪心
Matlab: build neural network
(零六)Flask有手就行——配置静态文件
i. Mx6ull driver development | 30 - use EC20 4G network card (migrate gobinet driver)
Des dizaines de milliards de données compressées à 600 go, tdengine est installé sur la plateforme mobile d'énergie de GCL
Unity iotar augmented reality tutorial
mysql 报错 mysqld:sort aborted:Server shutdown in progress 原因
Torch in pytoch Max() function analysis
在 SQL Server 中查找活动的 SQL 连接
薄膜铜箔导电电位测量
C语言·前缀树实现
SAP ABAP 系统进行数据库表查询的几种常用方法的试读版
VxWorks environment construction and learning
337.打家劫舍·动态规划
Torch in pytoch Analysis of nonzero() function
LDAP introduction
每日一题·648.单词替换·前缀树