2、项目使用的QT组件

前言

  • 在开始开发项目前,我们需要对项目中使用到的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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无休止符

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值