QString 是Qt的字符串类,与C++的std::string相比,不再使用ASCII编码。QString使用的Unicode编码。
QString 完全支持中文, 但是由于不同的技术可能会采用不同的编码。有时候也会遇到中文编码的一致性问题。
如果后续的学习或者工作中遇到编码出现中文乱码问题,请参考:
从此乱码是路人
QString中每字符都是一个16位QChar,而不是8位的char。
Qt中的QString对C++的std::string类进行了重写时,充分考虑到了C++程序员的编程习惯,因此QString几乎支持所有std::string的API。除此之外,也会新增一些API。
// int -> QString
// 参数1:要转换的数字
// 参数2:进制
// 返回值:转换后的QString 对象
QString number(int n, int base = 10
// int → QString
// 参数1:要转换的数字
// 参数2:进制
// 返回值:转换后的当前对象,支持链式调用
QString & QString::setNum(int n, int base = 10)
// QString -> int
// 参数1:转换成功或失败,成功设置成true,失败设置为false
// 参数2:进制
// 返回值:转换后的结果,失败的话返回0
int toInt(bool * ok = 0, int base = 10) const
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QString str = "你好";
qDebug()<
qDebug()<
//int->QString
int a=255;
qDebug()<
qDebug()<
qDebug()<
qDebug()<
//字符转int
bool resule = false;
str = "0";
qDebug()<,需要传参验证是否转换成功
qDebug() << resule;//true=1
str = "22aa";
qDebug()<
qDebug() << resule;//false = 0
}
Dialog::~Dialog()
{
delete ui;
}
不建议死记QString的API,因为数量较多且都有示例代码,只需要把常用函数的关键词记住即可:
Qt重写了C++的STL中的容器类,相比较于C++STL的容器,Qt的容器类更轻巧,安全和易于使用。因为Qt的容器类进行了速度和存储的优化,较少了可执行文件的生成体积。几乎兼容C++STL容器类所有API接口,并且是线程安全的,可以同时被多个线程所访问。
本次课程内容使用QList容器存储Student元素。Student是自定义数据类型,在Qt项目中创建一个C++类的文件。
2、在弹出的窗口中,选择“C++ class”,点击“选择”
在对象头文件中添加成员变量,以下操作可以自动在头函数中声明get和set成员函数,在对象文件中初始化相关成员函数。
student.h中声明,student.cpp中实现dialog.cpp中调用,调用时注意添加自定义类的头函数,并表明作用域。赋值
//遍历
for(int i=0;i < lis.count();i++)
{
Student s = lis.at(i);
qDebug()<
}
//C++迭代器 遍历,本质还是for循环,用iter迭代器指针遍历,指针开始指向对象头,结束指向对象尾。
for(QList::iterator iter = lis.begin();
iter != lis.end();iter++)
{
Student s = *iter;
qDebug()<
}
//JAVA迭代器,参数为容器对象
QListIterator iter(lis);
while(iter.hasNext())//判断当前迭代器指针后面是否有可用元素
{
Student s = iter.next(); // 向后移动迭代器指针并取出元素
qDebug()<
}
重新实现了STL中std::map类,QMap也兼容map类的API,也增加了一些新的Qt的API。
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
#include
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
QMap map;//创建一个栈内存对象
//插入数据
map.insert("姓名","张三");
map.insert("年龄","18");
map.insert("地址","济南");
map.insert("专业","总裁");
map.insert("身高","185");
map.insert("爱好","女");
//如果容器中的元素支持qDebug输出,则容器本身也支持输出
qDebug()<
Qt是一个跨平台的开发框架,所以必须要保证各个平台的数据类型长度保持一致,因此Qt为最常见的数据类型提供了新的定义。
在Qt的环境下,可以直接使用。
QVariant类型可以与Qt常见的数据类型完成相互转换,因此此类型的函数具有类似于多态的性质。
dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
qint64 a = 123;
QVariant v(a);
QString s = v.(); // 转换成字符串
qDebug() << s ; // 字符串:"123"
v = s;
int b = v.toInt(); // 转换成int
qDebug() << b ; // 整形:123
}
Dialog::~Dialog()
{
}
几乎相当于QList
QList str={"AAAA","BBBB","CCCC"};
Qt中用QDate类处理日期,使用QTime类处理时间,使用QDateTime类处理时间和日期。以QDateTime为例进行讲解。
需要注意的是,QDateTime的数据来源于系统日期和时间,所以修改系统时间会影响到QDateTime的数据。
// 返回1970年1月1日00:00:00到现在的毫秒数
qint64 QDateTime:: currentMSecsSinceEpoch()[static]
1、时间戳的作用,计算代码的运算时间。
时间戳可以作为随机数的种子。但是需要注意的是,我们的计算机的随机数都是伪随机。不是真正的随机数。计算机无法做到真正的随机数。
// 返回一个包含当前日期和时间的qDateTime对象
QDateTime QDateTime:: currentDateTime()[static]
// 参数为格式化输出年月日、时分秒
QString QDateTime:: toString(const QString & format) const
秒:ss
大写月份;MMMM
星期:dddd
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
#include
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
qint64 start = QDateTime::currentMSecsSinceEpoch();
ui->setupUi(this);
qDebug() << QDateTime::currentMSecsSinceEpoch() - start;
// 使用时间戳作为随机数的种子
qsrand(start);
// 生成随机数种子 生成(1-100)随机数
qDebug() << qrand()%101;
QDateTime dt = QDateTime::currentDateTime();
qDebug() << dt.toString("yyyy年MM月dd日 hh时mm分ss秒");
}
Dialog::~Dialog()
{
delete ui;
}
1、QTimeEdit
2、QDateEdit
4、QCalendar
QTimer类可以实现一个延时任务或者周期任务。
使用定时器,需要包含头文件#include
时间间隔,单位毫秒
是否是一次性
当前定时器的运行状态QLCDNumber 组件
使用这个组件,显示出11:31:30 这样的时间
// 构造函数 堆区开辟
QTimer:: QTimer(QObject * parent = 0)
// QLCDNumber 的显示槽函数
void display(const QString & s)[slot]
// 启动定时器,如果定时器已经存在,则会重启
void QTimer:: start()[slot]
// 定时器触发时,发送的信号
void QTimer:: timeout()[signal]
// 停止定时器
void QTimer:: stop()
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
#include
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QTimer *timer;
private slots:
void timeoutSlot();
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
// 创建定时器对象
timer = new QTimer(this);
// 提前刷新显示控件
timeoutSlot();
// 设置定时器参数(时间周期)
timer->setInterval(1000);
// 设置周期循环
timer->setSingleShot(false);
// 信号槽连接,连接要在定时器启动之前
connect(timer,SIGNAL(timeout()),this,SLOT(timeoutSlot()));
// 启动定时器
timer->start();
}
Dialog::~Dialog()
{
if(timer->isActive()) // 如果正在运行,则关闭
{
timer->stop();
}
delete ui;
delete timer;
}
void Dialog::timeoutSlot()
{
QString str = QDateTime::currentDateTime().toString("hh:mm:ss");
ui->lcdNumber->display(str);
}