Qt的常见数据类型
1.Qt的基本数据类型
对于Qt中的基本数据类型,虽然名称同C/C++中有所差异,但是其本质还是一致的。
可以通过源码查看其原型。
typedef int qint32;
源 码 很 清 晰 的 指 出 ,qint32
本质就是int,Qt使用typedef关键字在int的基础上进行了二次封装,让其更具Qt特色。
可 以 使 用 sizeof(type)
查看对应类型占用的内存大小。
类型 | 占用内存大小(位) | 说明 |
---|---|---|
bool | 8(bit)位 | true/false |
qint8 | 8(bit)位 | signed char |
qint16 | 16(bit)位 | signed short |
qint32 | 32(bit)位 | signed int |
qint64 | 64(bit)位 | long long int |
quint8 | 8(bit)位 | unsigned char |
quint16 | 16(bit)位 | unsigned short |
quint32 | 32(bit)位 | unsigned int |
quint64 | 64(bit)位 | unsigned long long int |
float | 32(bit)位 | 单精度浮点数 |
double | 64(bit)位 | 双精度浮点数 |
const char * | 32(bit)位 | 指向字符串常亮的指针,最后不能为0 |
2.Qt的基本数据类
Qt作为一个框架,包含非常多的类,其封装的特点都是大写“Q”开头,再加上实体类的名字。
类 | 说明 |
---|---|
QBitArray | 位数组 |
QBrush | 定义了QPainter绘制图形的填充模式 |
QByteArray | 字节数组 |
QColor | 颜色类 |
QDate | 日期类 |
QDateTime | 日期时间类 |
QFont | 字体类 |
QIcon | 图标类 |
QImage | 显示图像的类 |
QPicture | 处理图片格式的类 |
QPixmap | 处理PNG等格式图片的类 |
QTime | 提供时分秒的类 |
QPen | 画笔类,提供绘制线条的分格,厚度,颜色 |
QPoint | 坐标点类,提供处理X,Y,Z |
QSize | 尺寸类,宽度,高度 |
class Q_CORE_EXPORT QBitArray
{
friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const
QBitArray &);
friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &,
QBitArray &);
friend Q_CORE_EXPORT uint qHash(const QBitArray &key, uint seed)
Q_DECL_NOTHROW;
QByteArray d;
public:
inline QBitArray() Q_DECL_NOTHROW {}
explicit QBitArray(int size, bool val = false);
QBitArray(const QBitArray &other) : d(other.d) {}
inline QBitArray &operator=(const QBitArray &other) { d = other.d; return
*this; }
inline QBitArray(QBitArray &&other) Q_DECL_NOTHROW :
d(std::move(other.d)) {}
inline QBitArray &operator=(QBitArray &&other) Q_DECL_NOTHROW
{ qSwap(d, other.d); return *this; }
inline void swap(QBitArray &other) Q_DECL_NOTHROW { qSwap(d,
other.d); }
inline int size() const { return (d.size() << 3) - *d.constData(); }
inline int count() const { return (d.size() << 3) - *d.constData(); }
int count(bool on) const;
......
}
3.Qt的容器类
容器类都有一个比较共同的作用——存储“对象”。
虽然这些容器类的作用都是用来作为存储“对象”的容器,但是每个容器都是有自己的特点,比如是否有序,唯一,插入、查询效率的高低等,这些特点与容器的底层数据结构息息相关。
类 | 说明 |
---|---|
QHash<Key,T> | 提供散列表的字典(Dictionary)的模版类 |
QMap<Key,T> | 提供二叉搜索树(Binary Search tree)字典的模版类 |
QPair<T1,T2> | 处理成对存在的项目数据的模版类 |
QList | 为操作列表形态值提供的模版类 |
QLinkedList | 提供链表的模版类 |
QVector | 为操作动态QVector数组提供的类 |
QStack | 为使用包含push(),pop(),top()的栈提供的类 |
QQueue | 为使用enqueue(),dequeue()和head()操作FIFO结构的数据提供的类 |
QSet | 为利用基于散列快速查找提供的类 |
QMultiMap<Key,T> | 继承自QMap的类,可以映射多种值 |
QMultiHash<Key,T> | 继承自QHash的类,可以利用散列映射多种值 |
4.Qt的字符容器类
字符容器类也属于容器范畴,它主要用于存储字符。
类 | 说明 |
---|---|
QString | 字符串类,支持Unicode编码 |
QStringList | 字符串列表的集合类 |
QStringMatcher | 为查找与Unicode String字符串相对应的字符串提供的类 |
QStringRef | size()、position()、toString()等字符串包装(Wrapper)类 |
QChar | 支持16位Unicode字符(Character)的类 |
QByteArray | 支持字符数组的类,类似于网络文件传送、实现数据流时使用 |
QByteArrayMatcher | 利用QByterArray实现的字节数组索引查找对应字符串 |
QLatin1Char|QLatin1String | 支持US-ASCII/Latin-1编码字符串的类 |
QLocal | 将数字或字符显示方式转换为相应的多语言表达方式 |
QTextStream | 为写或读取文本提供的类,在文件中读写数据时使用 |
QString类源码剖析:
class Q_CORE_EXPORT QString
{
public:
typedef QStringData Data;
...
Data *d;
...
}
QStringData 的宏定义:
typedef QTypedArrayData<ushort> QStringData;
struct QTypedArrayData
QTypedArrayData的本质:
struct QTypedArrayData : QArrayData
{...}