04-1_Qt 5.9 C++开发指南_常用界面设计组件_字符串QString

本章主要介绍Qt中的常用界面设计组件,因为更多的是涉及如何使用,因此会强调使用,也就是更多针对实例,而对于一些细节问题,需要参考《Qt5.9 c++开发指南》进行学习。

文章目录

  • 1. 字符串与普通转换、进制转换
    • 1.1 可视化UI设计
    • 1.2 widget.h
    • 1.3 widget.cpp
  • 2. QString 的常用功能
    • 2.1 可视化UI设计
    • 2.2 widget.h
    • 2.3 widget.cpp

1. 字符串与普通转换、进制转换

图4-1是实例samp4_1 设计时的窗体,是基于QWidget 创建的可视化窗体。界面设计使用了布局管理,窗体上组件的布局是:上方的几个组件是一个 GridLayout,下方的9 个组件也是一个GridLayout,两个 GridLayout 和中间一个 VerticalSpacer又组成一个 VerticalLayout。

在这里插入图片描述

在布局设计时,要巧妙运用 VerticalSpacer 和 HorizontalSpacer,还要会设置组件的MaximumSize 和MinimumSize 属性,以取得期望的布局效果。例如,在图 4-1 中,两个 GridLayout 之间放了一个垂直方向的分隔,当窗体变大时,两个 GridLayout 的高度并不会发生变化;而如果不放置这个垂直分隔,两个 GridLayout的高度都会发生变化,GridLayout 内部组件的垂直距离会发生变化。

1.1 可视化UI设计

在这里插入图片描述

1.2 widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void on_btnCal_clicked();  //计算 按键单击响应

    void on_btnDec_clicked();   //十进制转换为其他进制

    void on_btnBin_clicked();   //二进制转换为其他进制

    void on_btnHex_clicked();   //十六进制转换为其他进制

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

1.3 widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include    <QString>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btnCal_clicked()
{ //计算 按键单击响应
    int num=ui->editNum->text().toInt(); //读取字符串为整数
    float price=ui->editPrice->text().toFloat();//读取字符串为浮点数

    float total=num*price;//相乘计算
    QString str;
//    str=str.setNum(total,'f',2); //浮点数2位小数
    str=str.sprintf("%.2f",total); //格式化输出浮点数
    ui->editTotal->setText(str);//在文本框里显示
}

void Widget::on_btnDec_clicked()
{ //读取十进制数,转换为其他进制
    int val=ui->editDec->text().toInt();//读取十进制数
    QString str=QString::number(val,16);// 显示为16进制 的字符串

    str=str.toUpper(); //转换为全大写字母
    ui->editHex->setText(str);//显示16进制字符串

    str=QString::number(val,2);// 显示2进制的字符串
    ui->editBin->setText(str);//显示二进制字符串
}

void Widget::on_btnBin_clicked()
{ //读取二进制数,转换为其他进制的数
    bool ok;

    int val=ui->editBin->text().toInt(&ok,2);//以二进制数读入

    QString str=QString::number(val,10);//数字显示为10进制字符串
    ui->editDec->setText(str);//显示10进制数字符串

    str=QString::number(val,16);//显示为十六进制字符串
    str=str.toUpper(); //全大写字母
    ui->editHex->setText(str);//显示十六进制字符串
}

void Widget::on_btnHex_clicked()
{//读取16进制数,转换为其他进制的数
    bool ok;

    int val=ui->editHex->text().toInt(&ok,16);//以十六进制数读入
    QString str=QString::number(val,10);// 显示为10进制字符串
    ui->editDec->setText(str);//显示为10进制字符串

    str=QString::number(val,2);// 显示二进制字符串
    ui->editBin->setText(str);//显示二进制字符串
}

2. QString 的常用功能

QString 是 Qt 编程中常用的类,除了用作数字量的输入输出之外,QString 还有很多其他功能,熟悉这些常见的功能,有助于灵活地实现字符串处理功能。
QString 存储字符串采用的是 Unicode 码,每一个字符是一个 16 位的 QChar,而不是8 位的char,所以 QString 处理中文字符没有问题,而且一个汉字算作是一个字符。

图4-2 是对 QString 常用函数的测试实例 samp4_2 的运行界面。下面在说明函数功能时,对于同名不同参数的函数,只说明某种参数下的使用实例。QString 还有很多功能函数没有在此介绍,在使用中如果遇到,可查询 Qt 的帮助文件。

在这里插入图片描述

2.1 可视化UI设计

在这里插入图片描述

2.2 widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_6_clicked();

    void on_pushButton_7_clicked();

    void on_pushButton_8_clicked();

    void on_pushButton_9_clicked();

    void on_pushButton_10_clicked();

    void on_pushButton_11_clicked();

    void on_pushButton_12_clicked();

    void on_pushButton_13_clicked();

    void on_pushButton_14_clicked();

    void on_pushButton_15_clicked();

    void on_pushButton_16_clicked();

    void on_pushButton_17_clicked();

    void on_pushButton_18_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

2.3 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()
{//append()函数
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();
    str1.append(str2);

    ui->edtResult->setText(str1);
}

void Widget::on_pushButton_2_clicked()
{//prepend()函数
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();
    str1.prepend(str2);

    ui->edtResult->setText(str1);
}

void Widget::on_pushButton_3_clicked()
{//contains()函数
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();

    bool chk;
    chk=str1.contains(str2);
    ui->checkBox->setChecked(chk);
    ui->checkBox->setText("contains()");
    ui->checkBox->sizeHint();
}

void Widget::on_pushButton_4_clicked()
{//count()函数
    QString str1=ui->comboBox1->currentText();
    int i=str1.count();
//    int i=str1.length();
    ui->spinBox->setValue(i);
    ui->LabSpin->setText("count()");
}

void Widget::on_pushButton_5_clicked()
{//size()函数
    QString str1;
    str1=ui->comboBox1->currentText();
    int i=str1.size();
    ui->spinBox->setValue(i);
    ui->LabSpin->setText("size()");

}

void Widget::on_pushButton_6_clicked()
{//endsWith()函数
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();

    bool chk;
    chk=str1.endsWith(str2);
    ui->checkBox->setChecked(chk);
    ui->checkBox->setText("endsWith()");
    ui->checkBox->sizeHint();
}

void Widget::on_pushButton_7_clicked()
{//indexOf()函数
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();

    int i;
    i=str1.indexOf(str2);
    ui->spinBox->setValue(i);
    ui->LabSpin->setText("indexOf()");
}

void Widget::on_pushButton_8_clicked()
{//isEmpty()函数
    QString str1;
    str1=ui->comboBox1->currentText();
    bool chk;
    chk=str1.isEmpty();
    ui->checkBox->setChecked(chk);
    ui->checkBox->setText("isEmpty()");
    ui->checkBox->sizeHint();
}

void Widget::on_pushButton_9_clicked()
{//lastIndexOf()函数
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();

    int i;
    i=str1.lastIndexOf(str2);
    ui->spinBox->setValue(i);
    ui->LabSpin->setText("lastIndexOf()");
}

void Widget::on_pushButton_10_clicked()
{//startsWith()函数
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();

    bool chk;
    chk=str1.startsWith(str2);
    ui->checkBox->setChecked(chk);
    ui->checkBox->setText("startsWith()");
    ui->checkBox->sizeHint();
}

void Widget::on_pushButton_11_clicked()
{//toUpper()函数
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=str1.toUpper();

    ui->edtResult->setText(str2);
}

void Widget::on_pushButton_12_clicked()
{//toLower()函数
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=str1.toLower();

    ui->edtResult->setText(str2);
}

void Widget::on_pushButton_13_clicked()
{//trimmed()函数
    QString str1;
    str1=ui->comboBox1->currentText();
    str1=str1.trimmed();

    ui->edtResult->setText(str1);

}

void Widget::on_pushButton_14_clicked()
{//section()函数
    int i;
    QString str1,str2,str3;
    str1=ui->comboBox1->currentText();
    i=ui->spinBox->value();
//    str2=str1.section('\\',2,2);
    str3=ui->comboBox2->currentText();
    if (QString::compare(str3,"\\",Qt::CaseInsensitive)==0)
        str2=str1.section('\\',i,i+1); //
    else
        str2=str1.section(str3,i,i+1); //

    ui->edtResult->setText(str2);
}

void Widget::on_pushButton_15_clicked()
{//left()函数
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    int v=ui->spinBox->value();
    str2=str1.left(v);
    ui->edtResult->setText(str2);
}

void Widget::on_pushButton_16_clicked()
{//right()函数
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    int cnt=str1.size();
    int v=ui->spinBox->value();
    str2=str1.right(cnt-v-1);
    ui->edtResult->setText(str2);
}

void Widget::on_pushButton_17_clicked()
{//simplified()函数
    QString str1;
    str1=ui->comboBox1->currentText();
    str1=str1.simplified();
    ui->edtResult->setText(str1);
}

void Widget::on_pushButton_18_clicked()
{//isNull()函数
    QString str1;
    str1=ui->comboBox1->currentText();
    bool chk;
    chk=str1.isNull();
    ui->checkBox->setChecked(chk);
    ui->checkBox->setText("isNull()");
    ui->checkBox->sizeHint();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/64921.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

万字长文解析深度学习中的术语

引言 新手在学习深度学习或者在看深度学习论文的过程中&#xff0c;有不少专业词汇&#xff0c;软件翻译不出来&#xff0c;就算是翻译出来也看不懂&#xff0c;因为不少术语是借用其他学科的概念&#xff0c;这里整理了一些在深度学习中常见的术语&#xff0c;并对一些概念进…

SpringSecurity5.7+最新案例 -- 授权 --

一、前提 书接上回 SpringSecurity5.7最新案例 – 用户名密码验证码记住我 本文 继续处理SpringSecurity授权 … 目前由 难 -> 简&#xff0c;即自定义数据库授权&#xff0c;注解授权&#xff0c;config配置授权 二、自定义授权 0. 数据准备 SET NAMES utf8mb4; SET …

CentOS软件包管理rpm、yum

一、软件包概述 Linux常见软件包分为两种&#xff0c;分别是源代码包、二进制文件包。源代码包是没有经过编译的包&#xff0c;需要经过GCC、C编译器编译才能运行&#xff0c;文件内容包含源代码文件&#xff0c;通常以.tar.gz、.zip、.rar结尾&#xff1b;二进制包无需编译&am…

APP外包开发的开发语言对比

在开发iOS APP时有两种语言可以选择&#xff0c;Swift&#xff08;Swift Programming Language&#xff09;和 Objective-C&#xff08;Objective-C Programming Language&#xff09;&#xff0c;它们是两种不同的编程语言&#xff0c;都被用于iOS和macOS等苹果平台的软件开发…

pytorch学习——卷积神经网络——以LeNet为例

目录 一.什么是卷积&#xff1f; 二.卷积神经网络的组成 三.卷积网络基本元素介绍 3.1卷积 3.2填充和步幅 3.2.1填充&#xff08;Padding&#xff09; 填充是指在输入数据周围添加额外的边界值&#xff08;通常是零&#xff09;&#xff0c;以扩展输入的尺寸。填充可以在卷…

Dockerfile构建Tomcat镜像

准备apache包和jdk并解压 [rootlocalhost tomcat]# ll 总用量 196728 -rw-r--r--. 1 root root 9690027 7月 17 2020 apache-tomcat-8.5.40.tar.gz -rw-r--r--. 1 root root 674 8月 2 20:19 Dockerfile -rw-r--r--. 1 root root 191753373 7月 17 2020 jdk-8u191-…

【Python基础教程】super()函数的正确使用方法

前言 大家早好、午好、晚好吖 ❤ ~欢迎光临本文章 1.super(本身类名,self).方法名(参数)这样就可以调用父类的方法和参数了,super()内也可不加参数 2.规律是super是按调用的次序执行&#xff0c;super后面的语句是逆向执行的。 有2段示例代码&#xff0c;不同的在于value有没…

基于短信宝API零代码实现短信自动化业务

场景描述&#xff1a; 基于短信宝开放的API能力&#xff0c;实现在特定事件&#xff08;如天气预警&#xff09;或定时自动发送短信&#xff08;本文以定时群发短信为例&#xff09;。通过Aboter平台如何实现呢&#xff1f; 使用方法&#xff1a; 首先创建一个IPaaS流程&…

解决Vue3 使用Element-Plus导航刷新active高亮消失

解决Vue3 使用Element-Plus导航刷新后active高亮消失的问题 启用路由模式会在激活导航时以 index 作为 path 进行路由跳转 使用 default-active 来设置加载时的激活项。 接下来打印一下选中项index和index路径&#xff0c; 刷新也是没有任何问题的&#xff0c;active不会消失…

python+django+mysql项目实践二(前端及数据库)

python项目实践 环境说明&#xff1a; Pycharm 开发环境 Django 前端 MySQL 数据库 Navicat 数据库管理 前端模板 添加模板 在templates下创建 views文件中添加 创建数据库 连接数据库 在setting文件中进行配置 创建表

SDXL-Stable Diffusion改进版

文章目录 1. 摘要2. 算法&#xff1a;2.1 结构&#xff1a;2.2 微小的条件机制2.3 多宽高比训练2.4 改进自编码器2.5 所有组合放到一起2.6 主流方案比较 3. 未来工作4. 限制 论文&#xff1a; 《SDXL: Improving Latent Diffusion Models for High-Resolution Image Synthesis…

APP外包开发的android开发模式

开发 Android 应用有多种方法&#xff0c;每种方法都有其优势和适用场景。综合考虑各自的特点&#xff0c;你可以根据项目的需求和团队的技能选择最合适的开发方法。今天和大家分享几种常见的开发方法以及它们之间的对比&#xff0c;希望对大家有所帮助。北京木奇移动技术有限公…

NeRF-SLAM: Real-Time Dense Monocular SLAM with Neural Radiance Fields 论文阅读

论文信息 题目&#xff1a;NeRF-SLAM: Real-Time Dense Monocular SLAM with Neural Radiance Fields 作者&#xff1a;Antoni Rosinol, John J. Leonard&#xff0c; Luca Carlone 代码&#xff1a;https://github.com/ToniRV/NeRF-SLAM 来源&#xff1a;arxiv 时间&#xff…

安装element-plus报错:Conflicting peer dependency: eslint-plugin-vue@7.20.0

VSCode安装element-plus报错&#xff1a; D:\My Programs\app_demo>npm i element-plus npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: vue/eslint-config-standard6.1.0 npm ERR! Found: eslint-plugin-vue8.7.1 npm E…

机器学习实战1-kNN最近邻算法

文章目录 机器学习基础机器学习的关键术语 k-近邻算法&#xff08;KNN&#xff09;准备&#xff1a;使用python导入数据实施kNN分类算法示例&#xff1a;使用kNN改进约会网站的配对效果准备数据&#xff1a;从文本文件中解析数据分析数据准备数据&#xff1a;归一化数值测试算法…

Go语言并发编程(千锋教育)

Go语言并发编程&#xff08;千锋教育&#xff09; 视频地址&#xff1a;https://www.bilibili.com/video/BV1t541147Bc?p14 作者B站&#xff1a;https://space.bilibili.com/353694001 源代码&#xff1a;https://github.com/rubyhan1314/go_goroutine 1、基本概念 1.1、…

鉴源论坛·观擎丨浅谈操作系统的适航符合性(上)

作者 | 蔡喁 上海控安可信软件创新研究院副院长 版块 | 鉴源论坛 观擎 社群 | 添加微信号“TICPShanghai”加入“上海控安51fusa安全社区” 01 源头和现状​​​​​​​ 在越来越多的国产机载系统研制中&#xff0c;操作系统软件的选择对后续开展研制以及适航举证活动带来…

码云 Gitee + Jenkins 配置教程

安装jdk 安装maven 安装Jenkins https://blog.csdn.net/minihuabei/article/details/132151292?csdn_share_tail%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22132151292%22%2C%22source%22%3A%22minihuabei%22%7D 插件安装 前往 Manage Jen…

基于Windows手动编译openssl和直接安装openssl

零、环境 win10-64位 VS2019 一、手动编译 前言&#xff1a;对于一般的开发人员而言&#xff0c;在 openssl 上下载已经编译好的 openssl 库&#xff0c;然后直接拿去用即可&#xff0c;&#xff0c;不用手动编译&#xff0c;{见下文直接安装}。。。对于一些开发人员&#…

Jmeter录制HTTPS脚本

Jmeter录制HTTPS脚本 文章目录 添加“HTTP代理服务器”设置浏览器代理证书导入存在问题 添加“HTTP代理服务器” 设置浏览器代理 保持端口一致 证书导入 点击一下启动让jmeter自动生成证书&#xff0c;放在bin目录下&#xff1a; 打开jmeter的SSL管理器选择刚刚生成的证书&…