当前位置:网站首页>QT learning 11 string classes in QT
QT learning 11 string classes in QT
2022-06-29 11:35:00 【A little black sauce】
Qt Study 11 Qt String class in
an issue rooted in history
- C Language I won't support it In the real sense character string
- C Language use A character array and A set of functions Implement string operation
- C Language I won't support it Custom type , therefore Cannot get string type
Solution
from C To C++ The evolutionary process of introduced Custom type
stay C++ Medium can Complete the definition of string type through class
C++ Whether the native type system in contains string types ?
Standard library STL
- STL yes In a sense Need and C++ Together with Standard library
- STL It's a set Completed by template technology C++ Class library
- STL Contains common Algorithm and data structure
- STL It contains String class
Qt VS STL
STL The specific Implementation depends on the compiler manufacturer
STL Of “ standard ” It's just that The interface is standard
- Same global function
- The same algorithm class and data structure class
- The same class member function
Different manufacturers The compiler of STL Differences exist
Depend on STL Developed C++ The behavior of programs on different platforms may vary
Pre development choices
- Whether the project needs to use Current library support ?
- STL?Qt?MFC? Private libraries ?
- Whether the project needs to be in Migration between different platforms ?
- Linux?Windows?Android?
- Whether the project needs Graphical user interface ?
- GUI Applications ? Command line application ? Background services ?
Qt String class in
- Qt String class in
- use Unicode code
- Use Implicit sharing technology To save memory and unnecessary data copies
- Cross platform use , There is no need to consider the platform compatibility of strings
QString VS string
- QString Direct support for The conversion between strings and numbers
- QString Direct support for String size comparison
- QString Direct support for Conversion between different character codes
- QString Direct support for std::string and std::wstring Mutual conversion of
- QString Direct support for Applications of regular expressions
- ...
Code example
#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;
}
Qt Medium QString
- QString stay Qt The library is almost ubiquitous
- be-all Qt Graphical user components all depend on QString
Add the message corresponding to the calculator program
/* QCalculatorUI.cpp */
// Content in previous code
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);
}
}
Summary
- application development Most of the time It's all going on string manipulation
- Qt Than STL It is more suitable for cross platform development scenarios
- Qt Medium QString Than STL in string It's bigger and easier to use
- Qt Graphical user components all depend on QString
- Required during project development Comprehensive selection of libraries to be used
边栏推荐
- 海泰前沿技术|隐私计算技术在医疗数据保护中的应用
- Multithreaded high concurrency server: three problems
- Online sql to htmltable tool
- Add notification announcements to send notifications to online users
- LVGL库入门教程 - 动画
- Easydss is deployed on Disk C, and the video playback cannot be played normally. How to solve this problem?
- X-FRAME-OPTIONS web page hijacking vulnerability
- 安全 创新 实践|海泰方圆受邀参加“数字时代的网信创新与价值共创”技术交流研讨会
- ModbusTCP协议网络学习型单路红外模块(中壳版)
- 数据分析方法与思维:漏斗分析
猜你喜欢

Multi thread communication between client and server (primary version)

专访 SUSS NiFT 负责人:Web3 的未来离不开“人人为我,我为人人”的治理

Modbus RTU 协议485学习型2路红外模块

The use of Fibonacci sequence and bubble sort in C language

How to obtain method parameter values through WinDbg

Opencv4.0 installation of raspberry pie for Xiaobai

喜报|海泰方圆通过CMMI-3资质认证,研发能力获国际认可

Pipeline aggregations管道聚合- parent-2

微博评论架构设计

Hit the industry directly! The first model selection tool in the industry was launched by the flying propeller
随机推荐
[digital signal modulation] realize signal modulation and demodulation based on am+fm+dsb+ssb, including Matlab source code
数据分析方法与思维:漏斗分析
what? It's amazing that you can read the whole comic book for free. You can't learn to be a money saver together
Adding sharding sphere5.0.0 sub tables to the ruoyi framework (adding custom sub table policies through SPI)
斐波那锲数列与冒泡排序法在C语言中的用法
Easydss is deployed on Disk C, and the video playback cannot be played normally. How to solve this problem?
After 22 years in office, the father of PowerShell will leave Microsoft: he was demoted by Microsoft for developing PowerShell
LeetCode 535 TinyURL的加密与解密[map] HERODING的LeetCode之路
Online text filter less than specified length tool
Is it safe to open a stock account online
9 easy to use JSON processing tools, greatly improving efficiency!
(JS) catch errors (exceptions)
Google Earth engine (GEE) - Gedi L2a vector canopy top height (version 2) global ecosystem data set
What are the main factors that affect the heat dissipation of LED packaging?
(JS) filter out keys with value greater than 2 in the object
(JS) handwritten deep copy
[untitled] I am plotting a big event
Exclusive interview with CTO: the company has deepened the product layout and accelerated the technological innovation of domestic EDA
Nuc980 started successfully
Mastering the clever use of some shell wildcards will make us write with half the effort