目录
一、Qt常见基本数据类型
注意:定义在 #include <QtGlobal>
类型名称 | 注释 | 备注 |
---|---|---|
qint8 | signed char | 有符号 8 位数据 |
qint16 | signed short | 16 位数据类型 |
qint32 | signed short | 32 位有符号数据类型 |
qint64 | long long int 或(__int64) | 64 位有符号数据类型,Windows 中定义为__int64 |
qintptr | qint32 或 qint64 | 指针类型 根据系统类型不同而不同,32 位系统为 qint32、64 位系统为 qint64 |
qlonglong long | long int 或(__int64) | Windows 中定义为__int64 |
qptrdiff | qint32 或 qint64 | 根据系统类型不同而不同,32 位系统为 qint32、64 位系统为 qint64 |
qreal | double 或 float | 除非配置了-qreal float 选项,否则默认为 double |
quint8 | unsigned char | 无符号 8 位数据类型 |
quint16 | unsigned short | 无符号 16 位数据类型 |
quint32 | unsigned int | 无符号 32 位数据类型 |
quint64 | unsigned long long int 或 (unsigned __int64) | 无符号 64 比特数据类型,Windows 中定义为 unsigned __int64 |
quintptr | quint32 或 quint64 | 根据系统类型不同而不同,32 位系统为 quint32、64 位系统为 quint64 |
qulonglong | unsigned long long int 或 (unsigned __int64) | Windows 中定义为__int64 |
uchar | unsigned char | 无符号字符类型 |
uint | unsigned int | 无符号整型 |
ulong | unsigned long | 无符号长整型 |
ushort | unsigned short | 无符号短整型 |
二、Qt 字符串类应用
2.1 操作字符串
1)QString 提供一个二元的“+”操作符,主要用于组合两个字符串。
#include <QCoreApplication> //Qt提供一个事件循环
#include <QDebug> //输出流
int main(int argc, char *argv[])
{
QCoreApplication a(argc,argv);
//1、QString 提供二元"+"操作符应用
QString str1 = "Hello";
str1=str1+"QString";
qDebug()<<str1;
qDebug()<<qPrintable(str1); //去掉双引号
QString str2="12345";
str2+="ABCDE";
qDebug()<<qPrintable(str2);
return a.exec();
}
2)QString::append() 直接在字符串末尾添加另一个字符串
QString str1 = "Good";
QString str2 = "bye";
str1.append(str2); //str1 = "Goodbye"
qDebug()<<qPrintable(str1);
str1.append("Hello !");
qDebug()<<qPrintable(str1); //str1 = "Goodbye Hello !"
3)组合字符串:QString::sprintf() 。与C++库里的sprintf()函数一样
QString strtemp;
strtemp.sprintf("%s","Hello ");
qDebug()<<qPrintable(strtemp);
strtemp.sprintf("%s","Hello world.");
qDebug()<<qPrintable(strtemp);
strtemp.sprintf("%s %s","Welcome","to you.");
qDebug()<<qPrintable(strtemp);
//输出
// Hello
// Hello world.
// Welcome to you.
4)字符串组合方式QString::arg()函数,该函数的重载可以处理多种数据类型。因为它类型安全,同时支持Unicode,可以改变%n参数顺序
QString strTemp;
strTemp=QString("%1 was born in %2.").arg("Tom").arg(2005);
qDebug()<<strTemp;
2.2 查询字符串
1)函数 QString::startsWith()判断一个字符串是否以某个字符串开头。Qt::CaseInsensitive 代表大小写不敏感;Qt::CaseSensitive 表示大小定敏感。对应关系函数:QString::endsWith()。
QString strTemp="How are you";
qDebug()<<strTemp.startsWith("How",Qt::CaseSensitive); // true
qDebug()<<strTemp.startsWith("are",Qt::CaseSensitive); // false
2)函数 QString::contains()判断一个指定的字符串是否出现过
QString strTemp="How are you";
qDebug()<<strTemp.contains("How",Qt::CaseSensitive);
3)QString::toInt() 函 数 将 字 符 串 转 换 为 整 型 数 值 。toDouble()、toFloat()、toLong()等
QString str="25";
bool isloop=true;
int hex=str.toInt(&isloop,16); //isloop=true hex=37
qDebug()<<"isloop="<<isloop<<","<<"hex="<<hex<<endl;
4)QString::compare()函数对两个字符串进行比较
int a1 = QString::compare("abcd","ABCD",Qt::CaseInsensitive); // 大小写不敏感
int b1 = QString::compare("about","Cat",Qt::CaseSensitive);
int c1 = QString::compare("abcd","Cat",Qt::CaseInsensitive);
cout<<"a1="<<","<<"b1="<<b1<<","<<"c1="<<c1<<endl;
5)Qt 将 QString 转换成 ASCII 码
QString str="ABC abc";
QByteArray bytes=str.toUtf8();
for(int i=0;i<str.size();i++)
qDebug()<<int(bytes.at(i));
三、QMap 类&QHash 类&QVector 类
3.1 QMap 类
QMap<Key,T>提供了·一个·从类型为Key的键到类型为T的值的映射。通常,QMap存储的数据形式是一个键对应一个值,并且按照Key的次序存储数据。为了能够支持一键多值的情况,QMap提供QMap<Key,T>::insertMulti()和QMap<Key,T>::values()函数。QMultiMap类来实例化一个QMap对象。
示例:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
// QMap 类
// 1:创建 QMap 实例,第一个参数为 QString 类型的键,第二个参数为 int
类型的值
QMap<QString, int> qmap;
// 插入数据信息,它有两方式进行操作
qmap["Chinese"] = 119;
qmap["English"] = 120;
qmap.insert("Math", 115);
qmap.insert("Physics", 99);
qmap.insert("Chemistry", 100);
qDebug() << qmap;
// 删除数据信息 key 键
qmap.remove("Chemistry");
qDebug() << qmap << endl;
// 遍历 QMap 类的实例:数据信息
// 1:迭代器(java 类型的迭代操作)
QMapIterator<QString, int> itr(qmap);
while (itr.hasNext())
{
itr.next();
qDebug() << itr.key() << ":" << itr.value();
}
// 2:STL 类型的迭代
qDebug() << endl;
QMap<QString, int>::const_iterator stritr = qmap.constBegin();
while (stritr != qmap.constEnd())
{
qDebug() << stritr.key() << ":" << stritr.value();
stritr++;
}
// key 键/T 键-->来查找
qDebug() << endl;
qDebug() << "key-->T:" << qmap.value("Math");
qDebug() << "T-->key:" << qmap.key(99) << endl;
// 修改键值
// 一个键对应一个值,再次调用 insert()函数将覆盖之前的值
qmap.insert("Math", 118);
qDebug() << qmap.value("Math");
// 查询是否包含某个键
qDebug() << endl;
qDebug() << "result=" << qmap.contains("Chinese");
qDebug() << "result=" << qmap.contains("Chemistry");
// 输出所有 QMap 实例化:Key 键和 T 值
qDebug() << endl;
QList<QString> aKeys = qmap.keys();
qDebug() << aKeys;
QList<int> aValues = qmap.values();
qDebug() << aValues;
// 一个键对应多个值
// 直接使用 QMultiMap 类来实例化一个 QMap 对象
qDebug() << endl;
QMultiMap<QString, QString> mulmap;
mulmap.insert("student", "no");
mulmap.insert("student", "name");
mulmap.insert("student", "sex");
mulmap.insert("student", "age");
mulmap.insert("student", "high");
mulmap.insert("student", "weight");
qDebug() << mulmap; // 从输出结果可以看出 mulmap 仍然是一个 QMap 对象
return a.exec();
}
3.2 QHash 类
QHash<Key,T>具有与QMap几乎完全相同的API。QHash维护着一张哈希表,哈希表的大小与QHash的数据项的数目相适应。
QHash以任意的顺序组织它的数据。当存储数据的顺序无关紧要,建议使用QHash作为存放数据的容器。
示例:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
// QHash 类
QHash<QString, int> qhash;
qhash["key 1"] = 3;
qhash["key 1"] = 8;
qhash["key 4"] = 4;
qhash["key 2"] = 2;
qhash.insert("key 3", 30);
QList<QString> list = qhash.keys();
for (int i = 0; i < list.length(); i++)
qDebug() << list[i] << "," << qhash.value(list[i]);
// QHash 内部的迭代器 QHashIterator 类
qDebug() << endl;
QHash<QString, int> hash;
hash["key 1"] = 33;
hash["key 2"] = 44;
hash["key 3"] = 55;
hash["key 4"] = 66;
hash.insert("key 3", 100);
QHash<QString, int>::const_iterator iterator;
for (iterator = hash.begin(); iterator != hash.end(); iterator++)
qDebug() << iterator.key() << "-->" << iterator.value();
return a.exec();
}
QMap 与 QHash 区别:
- QHash 与 QMap 的功能差不多,但 QHash 的查找速度更快;
- QMap 是按照键的顺序存储数据,而 QHash 是任意顺序存储的;
- QMap 的键必须提供"<”运算符,而 QHash 的键必须提供“=="运算符和一个名为 qHash()的全局散列函数
3.3 QVector 类
QVector<T>在相邻的内存当中存储给定数据类型T的一组数据。在QVector的前部或者中间位置进行插入操作的速度是很慢的,这是由于QVector存储数据的方式,导致这样的操作会导致内存中大量的数据被移动。
示例:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
// QVector 类
// QVector<T>是 Qt 的一个容器类
QVector<int> qvr;
// 第一种方式赋值
qvr << 10;
qvr << 20;
qvr << 30;
qvr << 40;
// 第二方式赋值
qvr.append(50);
qvr.append(60);
qvr.append(70);
qvr.append(80);
qvr.append(90);
qvr.append(100);
qDebug() << qvr << endl;
// 求出 QVector 类容器的实例化:元素个数
qDebug() << "qvr count=" << qvr.count() << endl;
// 遍历所有元素
for (int i = 0; i < qvr.count(); i++)
qDebug() << qvr[i];
// 删除 qvr 容器里面的元素
qDebug() << endl;
qvr.remove(0); // 删除第 0 个元素
for (int i = 0; i < qvr.count(); i++)
qDebug() << qvr[i];
qvr.remove(2, 3); // 从第 2 个元素开始,删除后面 3 个元素
qDebug() << endl;
for (int i = 0; i < qvr.count(); i++)
qDebug() << qvr[i];
// 判断容器是否包含某个元素
qDebug() << endl;
qDebug() << "result=" << qvr.contains(90);
qDebug() << "result=" << qvr.contains(901) << endl;
return a.exec();
}
四、QList 类&QLinkedList 类
4.1 QList 类
对于不同的数据类型,QList<T>采取不同的存储策略
- 如果T是一个指针类型或指针大小的基本类型(该基本类型占有的字节数和指针类型占有的字节数相同),QList<T>将数值直接存储到它的数组当中。
- 如果QList<T>存储对象的指针,则该指针指向实际存储对象
示例:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
// QList 类
QList<int> qlist; // 初始化一个空的 QList<int>列表
for (int i = 0; i < 10; i++)
qlist.insert(qlist.end(), i + 10);
qDebug() << qlist;
// 通过 QList<int>::iterator 读写迭代器
QList<int>::iterator x;
qDebug() << endl;
qDebug() << "Result1:";
for (x = qlist.begin(); x != qlist.end(); x++)
{
qDebug() << (*x);
*x = (*x) * 10 + 6;
}
// 初始化一个 QList<int>const_iterator 只读迭代器
qDebug() << endl;
qDebug() << "Result1:";
QList<int>::const_iterator qciter;
// 输出列表所有的值
for (qciter = qlist.constBegin(); qciter != qlist.constEnd(); qciter++)
qDebug() << *qciter;
// 向 qlist 添加元素
qlist.append(666);
QList<int>::iterator itr1;
qDebug() << endl;
qDebug() << "Result2:";
for (itr1 = qlist.begin(); itr1 != qlist.end(); itr1++)
qDebug() << *itr1;
// 查询 qlist 当中元素
qDebug() << endl;
qDebug() << "Result3:";
qDebug() << qlist.at(3);
qDebug() << qlist.contains(77);
qDebug() << qlist.contains(166);
// 修改 qlist 列表里面的元素值
qDebug() << endl;
qDebug() << "Result4:";
qlist.replace(5, 888);
qDebug() << qlist;
// 删除元素
qDebug() << endl;
qDebug() << "Result5:";
qlist.removeAt(0);
qlist.removeFirst();
qlist.removeAt(6);
qDebug() << qlist;
return a.exec();
}
4.2 QLinkedList 类
QLinkedList<T>是一个链式列表,它以非连续的内存块保存数据。QLinkedList<T>不能使用下标,只能使用迭代器访问它的数据项。与QList相比,当对于一个很大的列表进行插入操作时,QLinkedList具有更高的效率。
示例:
#include <QCoreApplication>
#include <QDebug>
#include <qlinkedlist.h>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
// QLinkedList 类
QLinkedList<QString> qAllMonth;
for (int i = 1; i <= 12; i++)
qAllMonth << QString("%1%2").arg("Month:").arg(i);
// 读写迭代器
qDebug() << "Result1:";
QLinkedList<QString>::iterator itrw = qAllMonth.begin();
for (; itrw != qAllMonth.end(); itrw++)
qDebug() << *itrw;
// 只读迭代器
qDebug() << endl << "Result2:";
QLinkedList<QString>::const_iterator itr = qAllMonth.constBegin();
for (; itr != qAllMonth.constEnd(); itr++)
qDebug() << *itr;
return a.exec();
}
QLinkedList 类不能通过索引方式访问元素(链表),保存大规模数量数据信息建议使用
QLinkedList(插入元素和删除元素速度快、效率高)
4.3 STL 风格迭代器遍历容器
容 器 类 | 只读迭代器类 | 读写迭代器类 |
---|---|---|
QList<T>,QQueue<T> | QList<T>::const_iterator | QList<T>::iterator |
QLinkedList<T> | QLinkedList<T>::const_iterator | QLinkedList<T>::iterator |
五、QVariant 类
QVariant 类本质为 C++联合(Union)数据类型,它可以保存很多Qt 类型的值,包括 QBrush、QColor、QString 等等。也能够存放Qt 的容器类型的值。
QVariant::StringList 是 Qt 定义的一个 QVariant::type 枚举类型的变量,其他常用的枚举类型变量如下表所示
变 量 | 对应的类型 | 变 量 | 对应的类型 |
---|---|---|---|
QVariant::Invalid | 无效类型 | QVariant::Time | QTime |
QVariant::Region | QRegion | QVariant::Line | QLine |
QVariant::Bitmap | QBitmap | QVariant::Palette | QPalette |
QVariant::Bool | bool | QVariant::List | QList |
QVariant::Brush | QBrush | QVariant::SizePolicy | QSizePolicy |
QVariant::Size | QSize | QVariant::String | QString |
QVariant::Char | QChar | QVariant::Map | QMap |
QVariant::Color | QColor | QVariant::StringList | QStringList |
QVariant::Cursor | QCursor | QVariant::Point | QPoint |
QVariant::Date | QDate | QVariant::Pen | QPen |
QVariant::DateTime | QDateTime | QVariant::Pixmap | QPixmap |
QVariant::Double | double | QVariant::Rect | QRect |
QVariant::Font | QFont | QVariant::Image | QImage |
QVariant::Icon | QIcon | QVariant::UserType | 用户自定义类型 |
示例:
//头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
// 定义学生结构体类型
struct student
{
int iNo;
QString strName;
int score;
};
Q_DECLARE_METATYPE(student)
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
};
#endif // MAINWINDOW_H
//源码文件
#include "mainwindow.h"
#include <QVariant>
#include <QDebug>
#include <QColor>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
QVariant qv1(298);
qDebug() << "qv1:" << qv1.toInt();
QVariant qv2("LingShengEDU");
qDebug() << "qv2:" << qv2.toString();
QMap<QString, QVariant> qmap;
qDebug() << endl;
qmap["int"] = 20000; // 整型
qmap["double"] = 99.88; // 浮点型
qmap["string"] = "Good"; // 字符串
qmap["color"] = QColor(255, 255, 0); // QColor 类型
// 输出:转换函数来处理
qDebug() << qmap["int"] << qmap["int"].toInt();
qDebug() << qmap["double"] << qmap["double"].toDouble();
qDebug() << qmap["string"] << qmap["string"].toString();
qDebug() << qmap["color"] << qmap["color"].value<QColor>();
// 创建一个字符串列表:QStringList
qDebug() << endl;
QStringList qsl;
qsl << "A" << "B" << "C" << "D" << "E" << "F";
QVariant qvsl(qsl); // 将列表存储在一个 QVariant 变量
if (qvsl.type() == QVariant::StringList)
{
QStringList qlist = qvsl.toStringList();
for (int i = 0; i < qlist.size(); i++) {
qDebug() << qlist.at(i); // 输出列表数据信息
}
}
// 结构体类型和 QVariant 类配合使用
qDebug() << endl;
student stu;
stu.iNo = 202201;
stu.strName = "sunny";
stu.score = 715;
// 使用静态方法保存数据
QVariant qstu = QVariant::fromValue(stu);
if (qstu.canConvert<student>()) // 判断是否可以转换操作
{
student temp = qstu.value<student>(); // 获取数据
student qtemp = qvariant_cast<student>(qstu); // 获取数据
qDebug() << "student:iNo=" << temp.iNo << ",strName=" << temp.strName << ",scor
e = "<<temp.score;
qDebug() << "student:iNo=" << qtemp.iNo << ",strName=" << qtemp.strName << ",sc
ore = "<<qtemp.score;
}
}
MainWindow::~MainWindow()
{
}