如何微信做演讲视频网站,网站备案批量查询,济南响应式网站建设,阿里巴巴建网站思维导图
学习目标
一、基础类型
因为Qt是一个C的框架#xff0c;因此C的语法和数据类型在Qt中都是被支持的#xff0c;但是Qt中也是定义了一些属于自己的数据类型#xff0c;不过#xff0c;好多数据类型都是对C的数据类型进行封装#xff0c;下面来简要介绍一下这些基…思维导图
学习目标
一、基础类型
因为Qt是一个C的框架因此C的语法和数据类型在Qt中都是被支持的但是Qt中也是定义了一些属于自己的数据类型不过好多数据类型都是对C的数据类型进行封装下面来简要介绍一下这些基础的数据类型
Qt的基本数据类型定义在 #include QtGlobal 中Qt的基本数据类型有 虽然在Qt中有属于自己的整形或者浮点型但是在编程过程中这些数据类型基本不使用常用的关键字是C/C中的intfloatdouble等。
二、Log输出
2.1 在调试窗口中输入日志 在Qt中进行log输出一般不使用C中的printf也不是使用C中的coutQt框架提供了专门用于日志输出的类头文件名为QDebug使用方法如下
// 包含QDebug的头文件
#include QDebug// 直接通过全局函数qDebug()就可以进行日志输出
qDebug() date: QDate::currentDate();
qDebug() Types: QString(String) QChar(x) QRect(0, 10, 50, 40); 扩展一下在Qt中和全局函数qDebug()类似的日志函数还有qWarning()qInfo() qCritical()
int number 100;
float i 11.11;qWarning() Number: number Bther value: i;
qInfo() Number: number Bther value: i;
qCritical() Number: number Bther value: i;
2.2 在终端窗口中输出日志 使用2.1的方法只能在项目调试过程中进行日志的输出如果不是通过IDE进行程序调试而是直接执行可执行程序在这种情况下是没有日志输出窗口的因此看不到任何日志输出。 默认情况下日志信息是不会打印到终端窗口的如果想要实现这样的效果必须在项目文件中添加相关的属性信息。 操作如下打开项目文件*.pro找到配置项 config添加 console 控制台属性 三、字符串类型
在Qt中不仅支持C,C中的字符串类型而且还在框架中定义了专属的字符串类型我们必须要掌握在Qt中关于这些类型的使用和相互之间的转换。
语言类型字符串类型Cchar* Cstd::stringchar*QtQByteArrayQString等
3.1 QByteArray 在Qt中QByteArray可以看做是C语言中char*的升级版本。我们在使用这种类型的时候可以通过这个类的构造函申请一块内存空间用于存储我们需要处理的字符串数据。
下面我们来看一看这个类中常用的一些API函数
// 构造函数// 1.构造空对象里面没有数据
QByteArray::QByteArray();// 2.将data中的size个字符进行构造得到一个字节数组对象
// 如果size -1, 函数内部自动计算字符串长度计算方式为strlen(data)
QByteArray::QByteArray(const char* data, int size -1);// 3.构造一个长度为size个字节并且每一个字节值都为ch的字节数组
QByteArray::QByteArray(int size, char ch);
// 尾部追加数据
QString QString::append(const QString str);
QString QString::append(const char *str);
QString QString::append(const QByteArray ba);
void QString::push_back(const QString other);
// 头部添加函数
QString QString::prepend(const QString str);
QString QString::prepend(const char *str);
QString QString::prepend(const QByteArray ba);
void QString::push_front(const QString other);
// 插入数据将ba插入带数组第i个字节的位置从0开始
QByteArray QByteArray::insert(int i, const QByteArray ba);
// 删除数据
// 从大字符串中删除len字符从第pos个字符的位置开始删除
QByteArray QByteArray::remove(int pos, int len)
// 从字符数组的尾部删除n个字节
void QByteArray::chop(int n)
// 从字节数组的pos位置将数组阶段前面的部分留下后边部分被删除
void QByteArray::truncate(int pos)
// 将对象中的数据清空使其为NULL
void QByteArray::clear();
// 字符串替换
// 将字节数组中的子字符串 before 替换为 after
// 其他重载函数的同名函数可以参考Qt帮助文档
QByteArray QByteArray::replace(const QByteArray before, const QByteArray after); // 判断字节数组中是否包含子字符串 ba, 包含返回 true, 否则返回 false
bool QByteArray::contains(const QByteArray ba) const;
bool QByteArray::contains(const char* ba) const;// 判断字节数组中是否包含子字符 ch, 包含返回 true, 否则返回 false
bool QByteArray::contains(char ch) const;// 判断字节数组是否以字符串 ba 开始是返回 true, 不是则返回 false
bool QByteArray::startsWith(const QByteArray ba) const;
bool QByteArray::startsWith(const char* ba) const;// 判断字节数组是否以字符 ch 开始的是返回 true, 不是返回 false
bool QByteArray::startsWith(char ch) const;// 判断字节数组是否以字符串 ba 结尾是返回 true, 不是返回false
bool QByteArray::endWith(const QByteArray ba) const;
bool QByteArray::endWith(const char* ba) const;// 判断字节数组是否以字符ch结尾是返回 true, 不是返回 false
bool QByteArray::endWith(const char ch) const;
// 使用迭代器
iterator QByteArray::begin();
iterator QByteArray::end();// 使用数的方式进行遍历
// i的取值范围 0 i size()
char QByteArray::at(int i) const;
char QByteArray::operator[](int i) const;
// 返回字节数组对象中字符的个数
int QByteArray::length() const;
int QByteArray::size() const;
int QByteArray::count() const;// 返回字节数组对象中字符串 ba 出现的次数
int QByteArray::count(const QByteArray ba) const;
int QByteArray::count(const char* ba) const;// 返回字节数组对象中字符串 ch 出现的次数
int QByteArray::count(char ch) const; 3.2 QString QString 也是封装了字符串但是内部的编码为 utf-8utf-8属于Unicode字符集它固定使用多个字节windows为2字节Linux为3个字节来表示一个字符这样就可以将世界上几乎所有语言的常用字符收录在其中。
下面是常用的API 3.3 两者之间的区别
四、QVariant
QVariant这个类很神奇或者是很方便的在很多时候需要几种不同的数据类型需要传递如果使用结构体不是很方便容器保存的也只是一种数据类型而QVariant可以存储多个类型。
QVariant这个类型充当着很常见的数据类型的联合。QVariant可以保存很多Qt的数据类型包括
QBrush、QColor、QCursor、QDateTime、QFont、QKeySequence、 QPalette、QPen、QPixmap、QPoint、QRect、QRegion、QSize和QString并且还有C的基本类型比如intfloat等。
4.1 标准类型
将标准数据类型转换为QVariant类型 4.2 自定义类型 除了标准类型我们自定义的类型也可以使用QVariant类进行封装被QVariant存储的数据类型需要有一个默认的构造函数和一个拷贝构造函数。为了实现这一个功能首先必须使用Q_DECLARE_METATYPE()宏。通常会将这个宏放在类的声明所在头文件的下面原型为Q_DECLARE_METATYPE(type)。
使用的具体步骤如下
第一步在头文件中声明
// *.h
struct MyTest
{int id;QString name;
};
// 自定义类型注册
Q_DECLARE_METATYPE(MyTest)
第二步在源文件中定义
MyTest t;
t.name 张三丰;
t.num 666;
// 值的封装
QVariant vt QVariant::fromValue(t);// 值的读取
if(vt.canConvertMyTest())
{MyTest t vt.valueMyTest();qDebug() name: t.name , num: t.num;
}
以上操作用到的QVariant类的API如下
// 如果当前QVariant对象可用转换为对应的模板类型 T, 返回true, 否则返回false
bool QVariant::canConvert() const;
// 将当前QVariant对象转换为实际的 T 类型
T QVariant::value() const;
五、位置和尺寸 在QT中我们常见的 点, 线, 尺寸, 矩形 都被进行了封装, 下边依次为大家介绍相关的类。 5.1 QPoint QPoint类封装了我们常用用到的坐标点 (x, y), 常用的 API如下: C 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// 构造函数
// 构造一个坐标原点, 即(0, 0)
QPoint::QPoint();
// 参数为 x轴坐标, y轴坐标
QPoint::QPoint(int xpos, int ypos);// 设置x轴坐标
void QPoint::setX(int x);
// 设置y轴坐标
void QPoint::setY(int y);// 得到x轴坐标
int QPoint::x() const;
// 得到x轴坐标的引用
int QPoint::rx();
// 得到y轴坐标
int QPoint::y() const;
// 得到y轴坐标的引用
int QPoint::ry();// 直接通过坐标对象进行算术运算: 加减乘除
QPoint QPoint::operator*(float factor);
QPoint QPoint::operator*(double factor);
QPoint QPoint::operator*(int factor);
QPoint QPoint::operator(const QPoint point);
QPoint QPoint::operator-(const QPoint point);
QPoint QPoint::operator/(qreal divisor);// 其他API请自行查询Qt帮助文档, 不要犯懒哦哦哦哦哦......5.2 QLine QLine是一个直线类, 封装了两个坐标点 (两点确定一条直线) 常用API如下: C 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// 构造函数
// 构造一个空对象
QLine::QLine();
// 构造一条直线, 通过两个坐标点
QLine::QLine(const QPoint p1, const QPoint p2);
// 从点 (x1, y1) 到 (x2, y2)
QLine::QLine(int x1, int y1, int x2, int y2);// 给直线对象设置坐标点
void QLine::setPoints(const QPoint p1, const QPoint p2);
// 起始点(x1, y1), 终点(x2, y2)
void QLine::setLine(int x1, int y1, int x2, int y2);
// 设置直线的起点坐标
void QLine::setP1(const QPoint p1);
// 设置直线的终点坐标
void QLine::setP2(const QPoint p2);// 返回直线的起始点坐标
QPoint QLine::p1() const;
// 返回直线的终点坐标
QPoint QLine::p2() const;
// 返回值直线的中心点坐标, (p1() p2()) / 2
QPoint QLine::center() const;// 返回值直线起点的 x 坐标
int QLine::x1() const;
// 返回值直线终点的 x 坐标
int QLine::x2() const;
// 返回值直线起点的 y 坐标
int QLine::y1() const;
// 返回值直线终点的 y 坐标
int QLine::y2() const;// 用给定的坐标点平移这条直线
void QLine::translate(const QPoint offset);
void QLine::translate(int dx, int dy);
// 用给定的坐标点平移这条直线, 返回平移之后的坐标点
QLine QLine::translated(const QPoint offset) const;
QLine QLine::translated(int dx, int dy) const;// 直线对象进行比较
bool QLine::operator!(const QLine line) const;
bool QLine::operator(const QLine line) const;// 其他API请自行查询Qt帮助文档, 不要犯懒哦哦哦哦哦......5.3 QSize 在QT中QSize类用来形容长度和宽度, 常用的API如下: C 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// 构造函数
// 构造空对象, 对象中的宽和高都是无效的
QSize::QSize();
// 使用宽和高构造一个有效对象
QSize::QSize(int width, int height);// 设置宽度
void QSize::setWidth(int width)
// 设置高度
void QSize::setHeight(int height);// 得到宽度
int QSize::width() const;
// 得到宽度的引用
int QSize::rwidth();
// 得到高度
int QSize::height() const;
// 得到高度的引用
int QSize::rheight();// 交换高度和宽度的值
void QSize::transpose();
// 交换高度和宽度的值, 返回交换之后的尺寸信息
QSize QSize::transposed() const;// 进行算法运算: 加减乘除
QSize QSize::operator*(qreal factor);
QSize QSize::operator(const QSize size);
QSize QSize::operator-(const QSize size);
QSize QSize::operator/(qreal divisor);// 其他API请自行查询Qt帮助文档, 不要犯懒哦哦哦哦哦......5.4 QRect 在Qt中使用 QRect类来描述一个矩形, 常用的API如下: C 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// 构造函数
// 构造一个空对象
QRect::QRect();
// 基于左上角坐标, 和右下角坐标构造一个矩形对象
QRect::QRect(const QPoint topLeft, const QPoint bottomRight);
// 基于左上角坐标, 和 宽度, 高度构造一个矩形对象
QRect::QRect(const QPoint topLeft, const QSize size);
// 通过 左上角坐标(x, y), 和 矩形尺寸(width, height) 构造一个矩形对象
QRect::QRect(int x, int y, int width, int height);// 设置矩形的尺寸信息, 左上角坐标不变
void QRect::setSize(const QSize size);
// 设置矩形左上角坐标为(x,y), 大小为(width, height)
void QRect::setRect(int x, int y, int width, int height);
// 设置矩形宽度
void QRect::setWidth(int width);
// 设置矩形高度
void QRect::setHeight(int height);// 返回值矩形左上角坐标
QPoint QRect::topLeft() const;
// 返回矩形右上角坐标
// 该坐标点值为: QPoint(left() width() -1, top())
QPoint QRect::topRight() const;
// 返回矩形左下角坐标
// 该坐标点值为: QPoint(left(), top() height() - 1)
QPoint QRect::bottomLeft() const;
// 返回矩形右下角坐标
// 该坐标点值为: QPoint(left() width() -1, top() height() - 1)
QPoint QRect::bottomRight() const;
// 返回矩形中心点坐标
QPoint QRect::center() const;// 返回矩形上边缘y轴坐标
int QRect::top() const;
int QRect::y() const;
// 返回值矩形下边缘y轴坐标
int QRect::bottom() const;
// 返回矩形左边缘 x轴坐标
int QRect::x() const;
int QRect::left() const;
// 返回矩形右边缘x轴坐标
int QRect::right() const;
六、日期和时间
6.1 QDate QDate类可以封装日期信息也可以通过这个类得到日期相关的信息, 包括:年, 月, 日。 C 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;// 日期对象格式化
/*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();6.2 QTime QTime类可以封装时间信息也可以通过这个类得到时间相关的信息, 包括:时, 分, 秒, 毫秒。 C 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
72
73
74
75
76
77
78
79
80
81
82// 构造函数
QTime::QTime();
/*h 取值范围 0 ~ 23m and s 取值范围 0 ~ 59ms 取值范围 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:00QTime t;t n.addSecs(70); // t 14:01:10t n.addSecs(-70); // t 13:58:50t n.addSecs(10 * 60 * 60 5); // t 00:00:05t n.addSecs(-15 * 60 * 60); // t 23:00:00// 从时间对象中取出 时/分/秒/毫秒
// 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;// 操作符重载 时间比较
bool QTime::operator!(const QTime t) const;
bool QTime::operator(const QTime t) const;
bool QTime::operator(const QTime t) const;
bool QTime::operator(const QTime t) const;
bool QTime::operator(const QTime t) const;
bool QTime::operator(const QTime t) const;// 静态函数 - 得到当前时间
[static] QTime QTime::currentTime();6.3 QDateTime QDateTime类可以封装日期和时间信息也可以通过这个类得到日期和时间相关的信息, 包括:年, 月, 日, 时, 分, 秒, 毫秒。其实这个类就是QDate 和 QTime 这两个类的结合体。 C 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// 构造函数
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() 函数
QString QDateTime::toString(const QString format) const;// 操作符重载 日期时间对象的比较
bool QDateTime::operator!(const QDateTime other) const;
bool QDateTime::operator(const QDateTime other) const;
bool QDateTime::operator(const QDateTime other) const;
bool QDateTime::operator(const QDateTime other) const;
bool QDateTime::operator(const QDateTime other) const;
bool QDateTime::operator(const QDateTime other) const;// 静态函数
// 得到当前时区的日期和时间(本地设置的时区对应的日期和时间)
[static] QDateTime QDateTime::currentDateTime();