当前位置: 首页 > news >正文

嵌入式Qt Qt中的字符串类

一.Qt中的字符串类

QString  vs  string:

QString在Qt库中几乎是无所不在的

所有的Qt图形用户组件都依赖于QString

实验1 :QString 初体验

#include <QDebug>
void Sample_1()
{QString s = "add";s.append(" ");    // "add "s.append("Qt");   // "add Qt"s.prepend(" ");   // " add Qt"s.prepend("C++"); // "C++ add Qt"qDebug() << s;s.replace("add", "&"); // "C++ & Qt"qDebug() << s;
}
void Sample_2()
{QString s = "";int index = 0;s.sprintf("%d. I'm %s, thank you!", 1, "Delphi Tang"); // "1. I'm Delphi Tang, thank you!"qDebug() << s;index = s.indexOf(",");s = s.mid(0, index);   // "1. I'm Delphi Tang"qDebug() << s;index = s.indexOf(".");s = s.mid(index + 1, s.length()); // " I'm Delphi Tang"s = s.trimmed();                  // "I'm Delphi Tang"qDebug() << s;index = s.indexOf(" ");s = s.mid(index + 1, s.length()); // "Delphi Tang"qDebug() << s;
}
void Sample_3(QString* a, int len)
{for(int i=0; i<len; i++){for(int j=i+1; j<len; j++){if( a[j] < a[i] ){QString tmp = a[i];a[i] = a[j];a[j] = tmp;}}}
}
int main()
{qDebug() << "Sample_1:";Sample_1();qDebug() << endl;qDebug() << "Sample_2:";Sample_2();qDebug() << endl;qDebug() << "Sample_3:";QString company[5] ={QString("Oracle"),QString("Borland"),QString("Microsoft"),QString("IBM"),QString("D.T.Software")};Sample_3(company, 5);for(int i=0; i<5; i++){qDebug() << company[i];}return 0;
}

运行结果:

实验2:为计算器实例添加消息响应–获取按键输入回显到QLineEdit 文本框

目录:

QCalculatorUI.h:

#ifndef _QCALCULATORUI_H_
#define _QCALCULATORUI_H_#include <QWidget>
#include <QLineEdit>
#include <QPushButton>class QCalculatorUI : public QWidget
{Q_OBJECT
private:QLineEdit* m_edit;QPushButton* m_buttons[20];QCalculatorUI();bool construct();
private slots:void onButtonClicked();
public:static QCalculatorUI* NewInstance();void show();~QCalculatorUI();
};#endif

QCalculatorUI.cpp:

#include "QCalculatorUI.h"
#include <QDebug>QCalculatorUI::QCalculatorUI() : QWidget(NULL, Qt::WindowCloseButtonHint)
{}bool QCalculatorUI::construct()
{bool ret = true;const char* btnText[20] ={"7", "8", "9", "+", "(","4", "5", "6", "-", ")","1", "2", "3", "*", "<-","0", ".", "=", "/", "C",};m_edit = new QLineEdit(this);if( m_edit != NULL ){m_edit->move(10, 10);m_edit->resize(240, 30);m_edit->setReadOnly(true);m_edit->setAlignment(Qt::AlignRight);}else{ret = false;}for(int i=0; (i<4) && ret; i++){for(int j=0; (j<5) && ret; j++){m_buttons[i*5 + j] = new QPushButton(this);if( m_buttons[i*5 + j] != NULL ){m_buttons[i*5 + j]->resize(40, 40);m_buttons[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);m_buttons[i*5 + j]->setText(btnText[i*5 + j]);connect(m_buttons[i*5 + j], SIGNAL(clicked()), this, SLOT(onButtonClicked()));}else{ret = false;}}}return ret;
}QCalculatorUI* QCalculatorUI::NewInstance()
{QCalculatorUI* ret = new QCalculatorUI();if( (ret == NULL) || !ret->construct() ){delete ret;ret = NULL;}return ret;
}void QCalculatorUI::show()
{QWidget::show();setFixedSize(width(), height());
}void QCalculatorUI::onButtonClicked()
{QPushButton* btn = (QPushButton*)sender();QString clickText = btn->text();if( clickText == "<-" ){QString text = m_edit->text();if( text.length() > 0 ){text.remove(text.length()-1, 1);m_edit->setText(text);}}else if( clickText == "C" ){m_edit->setText("");}else if( clickText == "=" ){}else{m_edit->setText(m_edit->text() + clickText);}
}QCalculatorUI::~QCalculatorUI()
{}

main.cpp:

#include <QApplication>
#include "QCalculatorUI.h"int main(int argc, char *argv[])
{QApplication a(argc, argv);QCalculatorUI* cal = QCalculatorUI::NewInstance();int ret = -1;if( cal != NULL ){cal->show();ret = a.exec();delete cal;}return ret;
}

 

http://www.lryc.cn/news/300735.html

相关文章:

  • 函数高级(C++)
  • jmeter-10调试取样器
  • C#,二进制数的按位旋转(Bits Rotate)算法与源代码
  • 解决ubuntu登录密码问题
  • Ubuntu忘记登录密码重置步骤
  • MySQL数据库基础(五):SQL语言讲解
  • python-使用ffmpeg批量修改文件的后缀名
  • 关于jupyter的一些小笔记
  • macOS 安装 conda
  • C++并发编程 -3.同步并发操作
  • 【打工日常】使用docker部署可视化工具docker-ui
  • LGAMEFI基于BPL公链开发的第一生态:开启RWA游戏娱乐与DeFi融合的新纪元
  • AI专题:5G-A扬帆风正劲,踏AI增长新浪潮
  • C++Linux网络编程:poll模型和简单使用
  • Excel模板2:进度条甘特图
  • 数据结构:4_二叉树
  • 设计模式之:状态模式(State Pattern)
  • 【微服安全】API密钥和令牌与微服务安全的关系
  • Mock.js
  • 【c++】list详细讲解
  • C#面:在.NET中 类 System.Web.UI.Page 可以被继承吗?
  • AI:128-基于机器学习的建筑物能源消耗预测
  • php基础学习之可变函数(web渗透测试关键字绕过rce和回调函数)
  • MongoDB聚合操作符:$acos
  • 开源PDF工具 Apache PDFBox 认识及使用(知识点+案例)
  • 微软.NET6开发的C#特性——委托和事件
  • 卷积神经网络的基本结构
  • python:使用GDAL库读取遥感影像指定行列数/经纬度坐标的像素值
  • Redis篇----第一篇
  • C语言-----用二维数组解决菱形的打印问题