当前位置:网站首页>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;
}
原网站

版权声明
本文为[无休止符]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq23001186/article/details/125476911