当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Interviewer: please introduce cache penetration, cache null value, cache avalanche and cache breakdown, which are easy to understand

【编译原理】山东大学编译原理复习提纲

How to write controller layer code gracefully?
![log4j:WARN No such property [zipPermission] in org.apache.log4j.RollingFileAppender.](/img/2c/425993cef31dd4c786f9cc5ff081ef.png)
log4j:WARN No such property [zipPermission] in org.apache.log4j.RollingFileAppender.

DMU software syntax highlighting VIM setting -- Learning Notes 6

Xiaomi Interviewer: let's talk about the proficient Registration Center for three days and three nights

One person manages 1000 servers? This automatic operation and maintenance tool must be mastered

Unsafe中的park和unpark

POI replacing text and pictures in docx

(已解决) npm突然报错 Cannot find module ‘D:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js‘
随机推荐
【LeetCode】Day90-二叉搜索树中第K小的元素
从5秒优化到1秒,系统飞起来了...
The interviewer of a large front-line factory asked: do you really understand e-commerce order development?
延时队列`DelayQueue`
JDBC transaction commit case
Gérer 1000 serveurs par personne? Cet outil d'automatisation o & M doit être maîtrisé
云服务器配置ftp、企业官网、数据库等方法
Transaction overview of tidb
磁选机是什么?
正斜杠反斜杠的由来
MySQL
How torch.gather works
tracepoint
JDBC读取Mysql数据列表
(resolved) NPM suddenly reports an error cannot find module 'd:\program files\nodejs\node_ modules\npm\bin\npm-cli. js‘
Manim math engine
Guava tutorial collect some cases and write Google tool classes slowly
OpenCV怎么下载?OpenCV下载后怎么配置?
Nature、science、cell旗下刊物
apifox学习