当前位置:网站首页>2、项目使用的QT组件
2、项目使用的QT组件
2022-06-27 07:09:00 【无休止符】
前言
- 在开始开发项目前,我们需要对项目中使用到的QT组件进行一些使用介绍
一、QString和QDebug
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QString>
#include <QDebug>
int main(int argc, char *argv[])
{
//QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//QGuiApplication app(argc, argv);
//QQmlApplicationEngine engine;
//engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
//if (engine.rootObjects().isEmpty())
// return -1;
//return app.exec();
QString str = "C:/";
QString str2 = "D:\\";
qDebug() << str << endl;
qDebug() << str2 << endl;
//拼接
str += "muying/";
str2.append("muying/");
qDebug() << str << endl;
qDebug() << str2 << endl;
//清空
str.clear();
//字符串查找
int pos = str2.indexOf("\\");
pos = str.indexOf("X");
//QStringLiteral是QString的宏,使用这个宏来计算字符串的长度
//截取字符串,param1=截取开始位置,param2=截取的长度
str2 = str2.mid(pos + 1, QStringLiteral("muying").length());
qDebug() << str << endl;
qDebug() << str2 << endl;
//数字 -> 字符串:转换方法1
str = QString::number(3.14);
//数字 -> 字符串:转换方法2
str.setNum(34);
//字符串 -> 数字
QString str3 = "123";
int i = str3.toInt();
qDebug("The value of str is: %s", qPrintable(str3));//qPrintable宏,转换为const char*
qDebug("The value of i is: %d", i);
str3 = "abc";
i = str3.toInt(); //虽然无法转换,但是不会报错,i=0
qDebug("The value of str is: %s", qPrintable(str3));
qDebug("The value of i is: %d", i);
return 0;
}
二、QScopedPointer智能指针
- 简单测试:加方法块就是为了出方法块后,从栈上释放
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QString>
#include <QDebug>
class SPA
{
public:
SPA()
{
qDebug() << "SPA::SPA()" << endl;
}
~SPA()
{
qDebug() << "SPA::~SPA()" << endl;
}
};
int main(int argc, char *argv[])
{
// QScopedPointer
{
QScopedPointer<int> i(new int(3));
QScopedPointer<SPA> spA(new SPA);
}
return 0;
}
- reset使用
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QString>
#include <QDebug>
class SPA
{
public:
SPA()
{
qDebug() << "SPA::SPA()" << endl;
}
~SPA()
{
qDebug() << "SPA::~SPA()" << endl;
}
void Print()
{
qDebug() << "SPA::Print()" << endl;
}
};
int main(int argc, char *argv[])
{
// QScopedPointer
{
QScopedPointer<int> i2(new int(3));
qDebug("The value of i2 is: %d", *i2);//3
i2.reset(new int(4));
qDebug("The value of i2 is: %d", *i2);//4
QScopedPointer<SPA> spA(new SPA);
spA->Print();
QScopedArrayPointer<SPA> spArr(new SPA[10]);//数组版本
}
return 0;
}
三、QThread多线程
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QString>
#include <QDebug>
#include <QThread>
class MyThread :public QThread
{
public:
MyThread()
{
isStop = false;
}
void CloseThread()
{
isStop = true;
}
void run()
{
while (true)
{
if (isStop) {
return; }
//tr将字符串做国际化标准化的处理
qDebug() << tr("MyThread id is: ") << QThread::currentThreadId();
sleep(1);
}
}
private:
bool isStop;
};
int main(int argc, char *argv[])
{
// 实现多线程
MyThread thread;
thread.start();
while (true)
{
;
}
thread.CloseThread();
return 0;
}
边栏推荐
- 攻防演习防御体系构建之第二篇之应对攻击的常用策略
- 面试官:你天天用 Lombok,说说它什么原理?我竟然答不上来…
- 【软件工程】山东大学软件工程复习提纲
- 面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?
- Win10 remote connection to ECS
- Idea方法模板
- 2022 cisp-pte (II) SQL injection
- 大学数据库mysql
- Yarn create vite reports an error 'd:\program' which is neither an internal or external command nor a runnable program or batch file
- R 中的 RNA-Seq 数据分析 - 调查数据中的差异表达基因!
猜你喜欢
多表联查--07--- Hash join
[graduation season] graduation is the new beginning of your life journey. Are you ready
(已解决) npm突然报错 Cannot find module ‘D:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js‘
POI replacing text and pictures in docx
Interviewer: do you have any plan to request a lot of data that does not exist in redis to destroy the database?
MySQL
再见了,敏捷Scrum
从5秒优化到1秒,系统飞起来了...
MySQL
mssql如何使用语句导出并删除多表数据
随机推荐
uview的安装和功能
postgreSQL在windows系统遇到权限否认(permission denied)
Talk about Domain Driven Design
将通讯录功能设置为数据库维护,增加用户名和密码
YOLOv6又快又准的目标检测框架 已开源
[leetcode] day90 the element with the smallest K in the binary search tree
Idea one click log generation
How to download opencv? How to configure opencv after downloading?
Use uview to enable tabbar to display the corresponding number of tabbars according to permissions
语音信号处理-概念(二):幅度谱(短时傅里叶变换谱/STFT spectrum)、梅尔谱(Mel spectrum)【语音的深度学习主要用幅度谱、梅尔谱】【用librosa或torchaudio提取】
Park and unpark in unsafe
ggplot2的自定义调色板
多表联查--07--- Hash join
Process termination (have you really learned recursion? Test your recursion Foundation)
JDBC parameterized query example
The song of cactus -- throwing stones to ask the way (1)
Memory barrier store buffer, invalid queue
聊聊领域驱动设计
(已解决) npm突然报错 Cannot find module ‘D:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js‘
[Kevin's third play in a row] is rust really slower than C? Further analyze queen micro assessment