Qt 学习记录
QT安装
QT依赖(Debian下):
sudo apt install libxcb-cursor0 libxcb-cursor-dev
QT下载速度慢解决方案
- 下载在线安装工具:Index of /qtproject/official_releases/online_installers/
- 使用
--mirror参数设置镜像源: https://mirrors.ustc.edu.cn/qtproject
示例:
./installer --mirror https://mirrors.ustc.edu.cn/qtproject
类型转换小总结
QString 转 QHostAddress
示例:从控件上获取输入的IP地址,并将其转换成QHostAddress类型
//获取ip地址
QString ipStr = ui->ipLineEdit->text();
//转换
QHostAddress ip(ipStr);
QHostAddress 转 QString
可以使用 QHostAddress 类中的成员函数 toString() 来达到此目的
函数原型:QString QHostAddress::toString() const
示例:将从套接字中获取的 ip 地址转换成 QString 类型
QString ip = socket->peerAddress().toString();
QString 转 QByteArray
创建一个 QString 类型的字符串
QString str = "This is a QString.";
可以使用 QString 类的成员方法 toLatin1()
QByteArray ba = str.toLatin1();
也可以使用 QString 类的另一个成员方法 toLocal8Bit()
QByteArray ba = str.toLocal8Bit();
QByteArray 转 QString
QString 类有一个构造函数 QString::QString(const QByteArray &ba),可以把 QByteArray 类型的对象作为参数传给它,以此来创建一个 QString 类型的对象
QByteArray ba = "ba";
QString str(ba);
数字类型转 QString
QString 类中有静态成员函数 number(),可以将一些数字类型转换为 QString 类型
函数原型:从函数原型可以看出,该函数支持多种数字类型转换为字符串,很方便
static QString number(int, int base=10);
static QString number(uint, int base=10);
static QString number(long, int base=10);
static QString number(ulong, int base=10);
static QString number(qlonglong, int base=10);
static QString number(qulonglong, int base=10);
static QString number(double, char f='g', int prec=6);
示例:整型转换成 QString
int num = 114514;
QString str = QString::number(num);
示例:从套接字中提取ip端口号并将其转换成 QString 类型显示在控件上
//获取端口号
QString port = QString::number(socket->peerPort());
//显示
ui->portLineEdit->setText(port);
获取信号的发出者
使用 [protected] QObject *QObject::sender() const 函数可以返回信号发出者的指针,不过这个指针是 OBject * 类型的,用的时候可能需要强制类型转换一下。
示例:获取信号发出者的指针
QTcpSocket * s = (QTcpSocket *)sender();
Qt使用MySQL
连接MySQL
首先,创建数据库对象
QSqlDatabase db; //要包含头文件<QSqlDatabase>
再使用静态成员方法 addDatabase 加载数据库驱动
db = QSqlDatabase::addDatabase("DataBaseName");
设置数据库参数
//设置数据库名称
db.setDatabaseName("DataBaseName");
//设置主机名
db.setHostName("IpAddress");
//设置用户名
db.setUserName("UserName");
//设置密码
db.setPassword("Password");
连接数据库,使用 open() 函数连接数据库,成功则返回 true,失败则返回 false
if (db.open()) {
QMessageBox::information(this, "连接提示", "连接成功");
}
else {
QMessageBox::warning(this, "连接提示", "连接失败");
}
使用SQL语句
首先创建一条SQL语句
QString sql = "select * from student";
再创建一个 QSqlQuery 对象
QSqlQuery query;
执行语句,使用 exec() 函数执行SQL语句,成功返回 true,失败返回 false
// 使用exec函数执行sql语句
if (query.exec(sql)) {
QMessageBox::information(this, "插入提示", "插入成功");
}
else {
QMessageBox::warning(this, "插入提示", "插入失败");
}
Object定时器和QTimer的使用
Object定时器
创建一个整型对象存放定时器的id
int myTimerId;
启动定时器:
startTimer() 函数会启动定时器并返回其id,例中的 this 是一个指向 QWidget 对象的指针,由于该类继承于 QObject 类,所以可以直接调用 startTimer() 函数。
//启动定时器,并约定每2000毫秒触发一次事件
myTimerId = this->startTimer(2000);
重写 timerEvent(QTimerEvent * event) 函数,该函数每隔2000ms会被执行一次。
函数声明
[virtual protected] void QObject::timerEvent(QTimerEvent *event)
重写
void MyClass::timerEvent(QTimerEvent *event) {
//在这里写下操作
}
关闭定时器:
使用 killTimer() 函数即可关闭定时器
原型:
void QObject::killTimer(int id)
示例:
this->killTimer(myTimerId);
QTimer定时器
创建定时器对象
QTimer * timer = new QTimer();
启动定时器,并约定timeout时间为2000ms
timer->start(2000);
上述定时器会每隔2000ms发送一个timeout信号,此时利用该信号调用槽函数来处理
connect(timer, &QTimer::timeout, this, 处理该信号的槽函数指针);
关闭定时器
timer->stop();
文件操作
读文件
可以使用 QFileDialog::getOpenFileName() 函数打开文件对话框并返回其文件名
该函数的第一个参数是父类,第二个参数是提示,第三个参数是默认打开的目录
QString fileName = QFileDialog::getOpenFileName(this, "请选择一个文件", "./");
根据 fileName 就可以打开文件了
//创建文件对象
QFile file(fileName);
//以只读方式打开文件
file.open(QIODevice::ReadOnly);
//读取文件内容
file.readAll();
//释放文件
file.close();
写文件
可以使用 QFileDialog::getSaveFileName() 函数打开文件对话框并返回其文件名
参数同上 QFileDialog::getOpenFileName()
QString fileName = `QFileDialog::getSaveFileName(this, "请选择一个文件", "./");
然后打开文件,写入文件
QFile file(fileName);
//以只写方式打开文件
file.open(QIODevice::WriteOnly);
QByteArray content = "xxxxxx";
//写入
file.write(content);
file.close();
其中读和写都将可能涉及到数据类型转换的问题,readAll() 的返回值和 write() 的参数都是 QByteArray 类型。
浙公网安备 33010602011771号