Qt common data types

1. Commonly used basic data types

 

common interface

//求绝对值,T是泛型,以下都是
T qAbs(const T &t)

//最大值和最小值,会把最大或最小的返回出来
T &qMax(const T &value1, const T &value2)
T &qMin(const T &value1, const T &value2)
    
//取中间值3个区中间的那个(比大小)
const T &qBound(const T &min, const T &value, const T &max)
    
//比较浮点数大小
bool qFuzzyCompare(float p1, float p2)
bool qFuzzyCompare(double p1, double p2)


 

//随机数,可以像C的随机数一样使用,能执行成功
//如果没有随机数种子(qsrand),每次通过qrand随机出来的数都是一样的
qsrand((uint)QTime::currentTime().msec);//获取时间的秒,当作随机数种子
qrand()%10;//随机0~9,和C语言讲的一样
//获取变量环境

QByteArray ba = qgetenv("Path");
qDebug() << ba ;

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓  

2、QSize 、QSizeF

QSize(int width, int height) //表示大小,宽高
QSizeF(qreal width, qreal height) 
    
    
QSize size(100, 10);
size.rwidth() += 20;//120,12
//有不带r的width,不带r不能这样修改


3、QPoint、QPointF

QPoint(int xpos, int ypos);
QPointF(qreal xpos, qreal ypos);

//类似和QSize一样的操作,只不过这个表示的坐标

4、QRect

QRect(const QPoint &topLeft, const QPoint &bottomRight)
QRect(const QPoint &topLeft, const QSize &size)
QRect(int x, int y, int width, int height)

 

5、QString

The QString class provides a string of Unicode characters. Note is a string of Unicode characters

5.1 Initialization

QString str="zhangsanlisi";

5.2 Added

QString str="sta";
str.prepend("six");//在前面追加
str.append("deu");//在后面追加
//也可以使用push_back、push_front

5.3 Replacement

QString str1="six sta edu";
QString str2="nine";
str1.replace(0,3,str2); //从第0个位子,把后面3个改为str2
//str1="nine sta edu";

5.4 Delete

QString str="five nine start edu";
str.remove(3,5);//从第三个位子删5个
qDebug()<<str;
str.remove(QChar('s'),Qt::CaseInsensitive);//不加第二个参,不区分大小写

qDebug()<<str;
str.remove("edu");//删除edu
qDebug()<<str;

5.5 Insertion

QString str="Fiv star";
str.insert(8,QString("t edu"));

5.6 Judging whether it is empty

//两种判断方式,注意区别

QString().isEmpty(); // returns true
QString("").isEmpty(); // returns true
QString("x").isEmpty(); // returns false
QString("abc").isEmpty(); // returns false

QString().isNull(); // returns true
QString("").isNull(); // returns false
QString("abc").isNull(); // returns false

5.7 Find the place where the string appears

QString str1="six SIX 666";
QString str2="six";

qDebug()<<str1.indexOf(str2);//返回0 找到了第一个匹配的
qDebug()<<str1.indexOf(str2,1);//从1的位子开始搜索 返回-1 区分大小写
qDebug()<<str1.indexOf(str2,3,Qt::CaseInsensitive);//返回4 区分大小写
qDebug()<<str1.indexOf(str2,5);//返回-1 没找到

5.8 Find if it contains a string

QString str = "SIX";
qDebug()<<str.contains("six", Qt::CaseInsensitive);//不区分大小写 返回true
qDebug()<<str.contains("six");//返回false

5.9 Find the number of string occurrences

QString str = "SIX six";
qDebug()<<str.count("six", Qt::CaseInsensitive);//返回2
qDebug()<<str.count("six");//返回1

5.10 String interception

QString str = "zhangsanlisi";
qDebug()<<str.left(7);//zhangsa 向左截取
qDebug()<<str.right(6);//anlisi 向右截取

5.11 mid() interception

QString str = "zhangsalisiwu";
qDebug()<<str.mid(7,4);//从第7个位子开始截取4个 lisi
qDebug()<<str.mid(7);//从第7个位子开始截取到最后 lisiwu

5.12 Number to QString

QString str;
qint32 val=10;
str=QString::number(val);//默认10进制
qDebug()<<str;//10

val=65535;
str=QString::number(val,16);//16进制 同理后面还可以是2 8
qDebug()<<str;//ffff

qreal num=1.0123456789;
str=QString::number(num);//默认情况 保留6位,整数位一起算进来
qDebug()<<str;//1.01235
str=QString::number(num,'g',8);//保留8位,'g'是默认参数,'f'是保留小数点后面的位数
qDebug()<<str;//1.0123457

5.13 Formatted input

qint32 age=18;
QString name="yimu";
qreal score=66.654321;

QString str=QString("姓名:%1,年龄:%2,成绩:%3").arg(name).arg(age).arg(score,0,'f',2);//保留小数点后2位
//%1,%2为格式占位符

qDebug()<<str;//姓名:yimu,年龄:18,成绩:66.65

5.14 Conversion between QString and std::string

QString str1="aaaaa";
string str2=str1.toStdString();//QString转std::string
cout<<str2;

//头文件iostream string 命名空间std
//选项->文本编辑器->行为->默认编码选system->ok
std::string str1="aaaaa";
QString str2=QString::fromStdString(str1);//std::string转QString
qDebug()<<str2;



std::string str1="aaaa";
QString str2 = str1.data();//str1.c_str();也行
qDebug()<<str2;

6、QByteArray

Provide a byte array (sequence), QByteArray can be used to store raw bytes (including "\0") and traditional 8-bit "\0" terminated strings. Using QByteArray is more convenient than using const char.

In IO operations, const char is often used in c language, and QByteArray is often used in Qt;

QByteArray ba1="adf";//直接赋值
qDebug()<<ba1;

qDebug()<<ba1.at(2);//通过at访问

QByteArray ba2;
ba2[0]='c';//方括号赋值,访问
qDebug()<<ba2;

6.1 Conversion between QByteArrar and char*

QByteArray ba("这是一个字符串");
const char *p=ba.constData();//QByteArray转const char *
qDebug()<<p;

const char *p="aaa";//const char *转QByteArray
QString s=QString(p);
QByteArray ba=s.toUtf8();//
qDebug()<<ba;

const char *p="aaa";//const char *转QByteArray
QByteArray ba(QByteArray::fromRawData(p,3));
qDebug()<<ba;

const char *p="aaa\0bbb\0ccc";
QByteArray ba(QByteArray::fromRawData(p,11));
qDebug()<<ba;
cout<<ba.constData();//返回值类型为 const char * 输出为aaa

7、QVariant

QVariant variables have no data type. The QVariant class is like a union of the most common Qt data types. A QVariant object only holds a single value of a single type at a time (some types may be multi-valued, such as a list of strings).

You can use the toT() (T represents a data type) function to convert the QVariant object to the T type and get its value. Here the toT() function will copy the previous QVariant object and then convert it, so the previous QVariant object will not change.

QVariant v=1.23;
qDebug()<<v;
qDebug()<<v.toInt();//输出1,转成了整型

v=13;
qDebug()<<v;
v="aaaaa";
qDebug()<<v;

The article is transferred from Blog Garden (Xuan Zhe): Common data types in qt- Xuan Zhe- Blog Garden

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓ 

Guess you like

Origin blog.csdn.net/QtCompany/article/details/132217025