当前位置:网站首页>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;
}
边栏推荐
- Unsafe中的park和unpark
- R 中的 RNA-Seq 数据分析 - 调查数据中的差异表达基因!
- DMU software syntax highlighting VIM setting -- Learning Notes 6
- POI export excle
- Memory barrier store buffer, invalid queue
- Gérer 1000 serveurs par personne? Cet outil d'automatisation o & M doit être maîtrisé
- oracle的similarity方法实现原理
- 从5秒优化到1秒,系统飞起来了...
- 多表联查--07--- Hash join
- 碎煤机crusher
猜你喜欢

(已解决) npm突然报错 Cannot find module ‘D:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js‘

SQL injection bypass (I)

The interviewer of a large front-line factory asked: do you really understand e-commerce order development?

从5秒优化到1秒,系统飞起来了...

yarn create vite 报错 ‘D:\Program‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件

Origin of forward slash and backslash

使用 Blackbox Exporter 测试网络连通性

Process termination (have you really learned recursion? Test your recursion Foundation)

POI replacing text and pictures in docx

Centos7.9 install MySQL 5.7 and set startup
随机推荐
volatile 和 synchronized 到底啥区别?
[Kevin's third play in a row] is rust really slower than C? Further analyze queen micro assessment
用XGBoost迭代读取数据集
在线文本数字识别列表求和工具
[openairinterface5g] rrcsetupcomplete for RRC NR resolution
How torch. gather works
Write an example of goroutine and practice Chan at the same time
Machine learning
Bean拷贝详解
使用 Blackbox Exporter 测试网络连通性
Centos7.9 install MySQL 5.7 and set startup
Vs how to configure opencv? 2022vs configuration opencv details (multiple pictures)
R 语言 基于关联规则与聚类分析的消费行为统计
【Kevin三连弹之三】Rust真的比C慢吗?进一步分析queen微测评
PostgreSQL encounters permission denied in Windows system
语音信号处理-概念(二):幅度谱(短时傅里叶变换谱/STFT spectrum)、梅尔谱(Mel spectrum)【语音的深度学习主要用幅度谱、梅尔谱】【用librosa或torchaudio提取】
碎煤机crusher
VNC Viewer方式的远程连接树莓派
Overview of database schema in tidb
一个人管理1000台服务器?这款自动化运维工具一定要掌握