Qt中的关键类

Qt对C++标准库中的许多数据类型进行了重新封装,加快了其运行速度,并且也保证了其线程安全

一、顺序容器

1.QList类

数组形式的线性表在Qt中的实现,提供了增、删、改、移动、替换等操作

  • 支持用索引访问

  • 插入、删除的时间复杂度是O(N)

insert(),replace(),removeAt(),move(),swap(),append(),preappend(),removeFirst(),removeLast()isEmpty(),size()…..

###2.QLinkedList类

链表形式的线性表在Qt中的实现

  • 不提供通过[]索引访问
  • 插入、删除的时间复杂度是O(1)

###3.QVector类

QList几乎完全一样,但性能比QList

4.QStack类

在Qt中的实现

  • 主要API:pop(),push()

5.QQueue类

队列在Qt中的实现

  • 主要API:enqueue(),dequeue()

##二、关联容器

1.QHash类

基于哈希表实现的键值对QHash<Key,T>

  • 查找速度更快
  • 内部没有对键排序

提供增删改查的操作:insert(key,val),remove(key),[key]

2.QMap类

基于红黑树(自平衡二叉搜索树)实现的键值对QMap<Key,T>

  • 内部存储是按照的顺序存储的
  • 默认情况下不允许一个键与多个值对应,这种情况应该使用QMultiMap

提供增删改查的操作:insert(key,val),remove(key),[key]

3.QSet类

基于QHash实现的集合QSet<T>

  • 由于基于哈希表,内部数值的存储顺序是不定的
  • 元素不能重复

contains()可以判断是否存在元素

三、QString类

它是Qt中重新封装的字符串类型,比标注C++的string强大了不少

1.数据操作

主要包括以下操作(其他重载的同名函数可参考Qt帮助文档, 此处略):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 尾部追加数据
QString &QString::append(const QString &str);
// 头部添加数据
QString &QString::prepend(const QString &str);
// 插入数据, 将 str 插入到字符串第 position 个字符的位置(从0开始)
QString &QString::insert(int position, const QString &str);
// 删除数据
// 从大字符串中删除len个字符, 从第pos个字符的位置开始删除
QString &QString::remove(int position, int n);
// 从字符串的尾部删除 n 个字符
void QString::chop(int n);
// 从字节串的 position 位置将字符串截断 (前边部分留下, 后边部分被删除)
void QString::truncate(int position);
// 将对象中的数据清空, 使其为null
void QString::clear();
// 字符串替换
// 将字节数组中的 子字符串 before 替换为 after
// 参数 cs 为是否区分大小写, 默认区分大小写
QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);

2.逻辑操作

1
2
3
4
5
6
7
8
// 参数 cs 为是否区分大小写, 默认区分大小写
// 判断字符串中是否包含子字符串 str, 包含返回true, 否则返回false
bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
// 判断字符串是否以字符串 ba 开始, 是返回true, 不是返回false
bool QString::startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
// 判断字符串是否以字符串 ba 结尾, 是返回true, 不是返回false
bool QString::endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;

3.查看长度

1
2
3
4
5
6
7
8
// 返回字节数组对象中字符的个数 (字符个数和字节个数是不同的概念)
int QString::length() const;
int QString::size() const;
int QString::count() const;

// 返回字节串对象中 子字符串 str 出现的次数
// 参数 cs 为是否区分大小写, 默认区分大小写
int QString::count(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;

4.类型转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 将int, short, long, float, double 转换为 QString 类型
QString &QString::setNum(int n, int base = 10);
[static] QString QString::number(long n, int base = 10);

// 将 QString 转换为 int, short, long, float, double 类型
int QString::toInt(bool *ok = Q_NULLPTR, int base = 10) const;
short QString::toShort(bool *ok = Q_NULLPTR, int base = 10) const;
long QString::toLong(bool *ok = Q_NULLPTR, int base = 10) const
float QString::toFloat(bool *ok = Q_NULLPTR) const;
double QString::toDouble(bool *ok = Q_NULLPTR) const;

// 将标准C++中的 std::string 类型 转换为 QString 类型
[static] QString QString::fromStdString(const std::string &str);
// 将 QString 转换为 标准C++中的 std::string 类型
std::string QString::toStdString() const;

// QString -> QByteArray
// 转换为本地编码, 跟随操作系统
QByteArray QString::toLocal8Bit() const;
// 转换为 Latin-1 编码的字符串 不支持中文
QByteArray QString::toLatin1() const;
// 转换为 utf8 编码格式的字符串 (常用)
QByteArray QString::toUtf8() const;

// 所有字符转换为大写
QString QString::toUpper() const;
// 所有字符转换为小写
QString QString::toLower() const;

5.动态拼接

1
2
3
4
5
6
7
8
9
10
11
12
//返回一个该字符串的拷贝,并且最低位的占位符被a替代
QString QString::arg(const QString &a,
int fieldWidth = 0,
QChar fillChar = QLatin1Char( ' ' )) const;

// 示例程序
int i; // 假设该变量表示当前文件的编号
int total; // 假设该变量表示文件的总个数
QString fileName; // 假设该变量表示当前文件的名字
// 使用以上三个变量拼接一个动态字符串
QString status = QString("Processing file %1 of %2: %3")
.arg(i).arg(total).arg(fileName);

四、QVariant类

QVariant类是多种不同数据类型的‘联合’,有的时候需要数据在几种不同的数据类型之间转换,可以用Qvariant类做中介,几乎所有的Qt定义的基础数据类型都可以转成QVariant,处理完后再转成别的数据类型即可

1.标准类型转成QVariant

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 这类转换需要使用QVariant类的构造函数, 由于比较多, 大家可自行查阅Qt帮助文档, 在这里简单写几个
QVariant::QVariant(int val);
QVariant::QVariant(bool val);
......

// 使用设置函数也可以将支持的类型的数据设置到QVariant对象中
// 这里的 T 类型, 就是QVariant支持的类型
void QVariant::setValue(const T &value);
// 该函数行为和 setValue() 函数完全相同
[static] QVariant QVariant::fromValue(const T &value);
// 例子:
QVariant v;
v.setValue(5);

QVariant v = QVariant::fromValue(5);

###2.QVariant转成标准类型

1
2
3
4
5
6
7
bool QVariant::toBool() const;
QByteArray QVariant::toByteArray() const;
double QVariant::toDouble(bool *ok = Q_NULLPTR) const;
float QVariant::toFloat(bool *ok = Q_NULLPTR) const;
int QVariant::toInt(bool *ok = Q_NULLPTR) const;
QString QVariant::toString() const;
......

五、日期和时间

###1.QDate类

QDate类可以封装日期信息,也可以通过这个类得到日期相关的信息, 包括:年, 月, 日

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 构造函数
QDate::QDate();
QDate::QDate(int y, int m, int d);

// 公共成员函数
// 重新设置日期对象中的日期
bool QDate::setDate(int year, int month, int day);
// 给日期对象添加 ndays 天
QDate QDate::addDays(qint64 ndays) const;
// 给日期对象添加 nmonths 月
QDate QDate::addMonths(int nmonths) const;
// 给日期对象添加 nyears 月
QDate QDate::addYears(int nyears) const;

// 得到日期对象中的年/月/日
int QDate::year() const;
int QDate::month() const;
int QDate::day() const;
void QDate::getDate(int *year, int *month, int *day) const;

//转字符串时必须用下面这些参数的组合,比如"yy-M-d"
/*
d - The day as a number without a leading zero (1 to 31)
dd - The day as a number with a leading zero (01 to 31)
ddd - The abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses the system locale to localize the name, i.e. QLocale::system().
dddd - The long localized day name (e.g. 'Monday' to 'Sunday'). Uses the system locale to localize the name, i.e. QLocale::system().
M - The month as a number without a leading zero (1 to 12)
MM - The month as a number with a leading zero (01 to 12)
MMM - The abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses the system locale to localize the name, i.e. QLocale::system().
MMMM - The long localized month name (e.g. 'January' to 'December'). Uses the system locale to localize the name, i.e. QLocale::system().
yy - The year as a two digit number (00 to 99)
yyyy - The year as a four digit number. If the year is negative, a minus sign is prepended, making five characters.
*/
QString QDate::toString(const QString &format) const;

// 操作符重载 ==> 日期比较
bool QDate::operator!=(const QDate &d) const;
bool QDate::operator<(const QDate &d) const;
bool QDate::operator<=(const QDate &d) const;
bool QDate::operator==(const QDate &d) const;
bool QDate::operator>(const QDate &d) const;
bool QDate::operator>=(const QDate &d) const;

// 静态函数 -> 得到本地的当前日期
[static] QDate QDate::currentDate();

2.QTime类

QTime类可以封装时间信息,也可以通过这个类得到时间相关的信息, 包括:时, 分, 秒, 毫秒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 构造函数
QTime::QTime();
/*
h ==> 取值范围: 0 ~ 23
m and s ==> 取值范围: 0 ~ 59
ms ==> 取值范围: 0 ~ 999
*/
QTime::QTime(int h, int m, int s = 0, int ms = 0);

// 公共成员函数
// Returns true if the set time is valid; otherwise returns false.
bool QTime::setHMS(int h, int m, int s, int ms = 0);
QTime QTime::addSecs(int s) const;
QTime QTime::addMSecs(int ms) const;

// 示例代码
QTime n(14, 0, 0); // n == 14:00:00
QTime t;
t = n.addSecs(70); // t == 14:01:10

// 从时间对象中取出 时/分/秒/毫秒
// Returns the hour part (0 to 23) of the time. Returns -1 if the time is invalid.
int QTime::hour() const;
// Returns the minute part (0 to 59) of the time. Returns -1 if the time is invalid.
int QTime::minute() const;
// Returns the second part (0 to 59) of the time. Returns -1 if the time is invalid.
int QTime::second() const;
// Returns the millisecond part (0 to 999) of the time. Returns -1 if the time is invalid.
int QTime::msec() const;


// 时间格式化
/*
-- 时 --
h ==> The hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hh ==> The hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
H ==> The hour without a leading zero (0 to 23, even with AM/PM display)
HH ==> The hour with a leading zero (00 to 23, even with AM/PM display)
-- 分 --
m ==> The minute without a leading zero (0 to 59)
mm ==> The minute with a leading zero (00 to 59)
-- 秒 --
s ==> The whole second, without any leading zero (0 to 59)
ss ==> The whole second, with a leading zero where applicable (00 to 59)
-- 毫秒 --
zzz ==> The fractional part of the second, to millisecond precision,
including trailing zeroes where applicable (000 to 999).
-- 上午或者下午
AP or A ==> 使用AM/PM(大写) 描述上下午, 中文系统显示汉字
ap or a ==> 使用am/pm(小写) 描述上下午, 中文系统显示汉字
*/
QString QTime::toString(const QString &format) const;

// 阶段性计时
// 过时的API函数
// 开始计时
void QTime::start();
// 计时结束,返回时间
int QTime::elapsed() const;
// 重新计时
int QTime::restart();

// 推荐使用的API函数
// QElapsedTimer 类
void QElapsedTimer::start();
qint64 QElapsedTimer::restart();
qint64 QElapsedTimer::elapsed() const;

// 静态函数 -> 得到当前时间
[static] QTime QTime::currentTime();

3.QDateTime类

QDateTime类可以封装日期和时间信息,也可以通过这个类得到日期和时间相关的信息, 包括:年, 月, 日, 时, 分, 秒, 毫秒。其实这个类就是QDateQTime 这两个类的结合体。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 构造函数
QDateTime::QDateTime();
QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec = Qt::LocalTime);

// 设置日期
void QDateTime::setDate(const QDate &date);
// 设置时间
void QDateTime::setTime(const QTime &time);
// 给当前日期对象追加 年/月/日/秒/毫秒, 参数可以是负数
QDateTime QDateTime::addYears(int nyears) const;
QDateTime QDateTime::addMonths(int nmonths) const;
QDateTime QDateTime::addDays(qint64 ndays) const;
QDateTime QDateTime::addSecs(qint64 s) const;
QDateTime QDateTime::addMSecs(qint64 msecs) const;

// 得到对象中的日期
QDate QDateTime::date() const;
// 得到对象中的时间
QTime QDateTime::time() const;

// 日期和时间格式, 格式字符参考QDate 和 QTime 类的 toString() 函数
// 可以只有日期或时间,也可以都有,只要符合format规定的就行了
QString QDateTime::toString(const QString &format) const;

// 静态函数
// 得到当前时区的日期和时间(本地设置的时区对应的日期和时间)
[static] QDateTime QDateTime::currentDateTime();