Qt基础知识汇总(持续刷新)

1、获取文件的绝对路径

获取绝对路径

#include<QFileInfo>
QString path = "/home/user/file.txt"
QFileInfo fi = QFileInfo(path);
QString absPath = fi.absolutePath();  // 提取绝对路径

获取文件名称

#include<QFileInfo>
QString path = "/home/user/file.txt"
QFileInfo fi = QFileInfo(path);
QString absPath = fi.fileName();  // 提取绝对路径

2、判断文件是否存在

QString file = "/home/user/file.txt"
if (QFile::exists(file)) {
	qDebug()<<"file is exists";
}

3、判断路径是否存在

QString path = "/home/user"
if (QDir::exists(path )) {
	qDebug()<<"dir is exists";
}

4、注册结构体类型

typedef struct {
	int name;
	int id;
} StuInfo;

qRegisterMetaType<StuInfo>("StuInfo"); 

5、QString 和string的转换

QString -> string:

string str = "test";
QString qstr = QString::fromStdString(str);

string -> QString

QString qstr = "test";
string str = qstr.toStdString();

QString -> char *

QString qstr = "test";
string str = qstr.toStdString().c_str();

char * -> QString


6、QVector 遍历方法

QVector<QString> vec;
QVector<QString>::iterator itor;
for (itor = vec.begin(); itor != vec.end(); itor++) {
	QString tmp = *itor;  // 记得用*itor。
	qDebug()<<tmp << (*itor)<<endl;
}

7、QThread 用法

TestClass *obj = new TestClass();
QThread *thd = new QThread;
obj->moveToThread(thd);
thd->start();

8、QString 左侧或右侧截取子字符串

QString str = "12345678";
QString left3str = str.left(3); // 结果: “123”
QString right3str = str.right(3); // 结果:“678”

9、linux下 QT依赖库打包

通过ldd命令进行打包

ldd exeName > dependLibs.txt
awk '{print $3}' dependLibs.txt > libsPath.txt
cat libsPath.txt | while read line
 
do
 echo "line->${line}"
 cp $line ./
done
相关链接:https://blog.csdn.net/my393661/article/details/83502540

10、linux QT开发中命令行需要输入密码操作

1) echo 当前用户密码 | sudo -S 你的命令
2) echo "PASSWORD" | sudo -S sh -c " CMD1; CMD2;..."
QString cmd = "echo 123456 | sudo -S apt-get install";
相关链接:https://blog.csdn.net/qq_34207847/article/details/89029592

11、QString 和 数值 转换

QString 转int : toInt()
QString 转float: toFloat()
QString 转double: toDouble()
QString 转short: toShort()
QString 转long: toLong()
QString 转long long: toLongLong()

int, float, double, short, long, long long 转 QString:QString::number()

QString str = "100";
int t = str.toInt();  // QString -> int
t = 200;
str = QString::number(t); // int -> QString;

12、QString 和 json转换

QJsonDocument、QJsonParseError、QJsonObject、QJsonArray、QJsonValue
相关链接
相关链接2

13、QString 和 QByteArray转换

QString str = "aaaa";
QByteArray bytes = str.toUtf8();

QString s = QString::fromUtf8(bytes);

14、Qt启动子进程,关闭父进程,子进程仍然存在

QProcess::startDetached("/home/test");

15、Qt 读写*.ini文件

QSettings *m_setting;
m_setting = new QSettings("./test.ini",QSettings::IniFormat);
m_setting->setValue("mac", "aa:bb:cc:dd:ee:ff");       // 写入值
QString value = m_setting->value("mac").toString();   // 获取值

16、Qt 文件创建和删除

创建:
QFile file("/home/test.txt");
if (!file.exists()) {
	file.open(QIODevice::WriteOnly|QIODevice::Text);  // 创建文件
}

删除:
if (file.exists()) {
	file.remove();
}

17、状态机

QStateMachine、QState、QEventTransition

18、Qt 使用sleep等待函数

QThread::sleep(seconds)

QThread::sleep(3);      // 秒级等待
QThread::msleep3000// 毫秒级等待

19、Qt 文件读取和写入

read:
QFile file("/home/test.txt");
if (file.exists()) {
	if (file.open(QIODevice::ReadWrite)) {
		QByteArray ba;
		ba = file.readAll();
		file.close();
	}
}

write:
if (file.exists()) {
	if (file.open(QIODevice::WriteOnly)) {
		QString str = "test";
		file.write(str.toStdString().data());
		file.close();
	}
}

20、Qt 事件循环:

1 QCoreApplicaton::exec()
2 QApplication::exec()
3 QDialog::exec()
4 QThread::exec()
5 QDrag::exec()
6 QMenu::exec()

21、获取本地时间

QDateTime current_date_time =QDateTime::currentDateTime();
QString current_date =current_date_time.toString(“yyyy.MM.dd hh:mm:ss.zzz ddd”);

获取日期加时间
QDateTime current_date_time =QDateTime::currentDateTime();
QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz ddd");
获取时分秒
QTime curTime =QTime::currentTime();
int hour = curTime.hour();//当前的小时
int minute = curTime.minute();//当前的分
int second = curTime.second();//当前的秒
int msec = curTime.msec();//当前的毫秒

参考链接

22、 async异步处理

void dataCopy(vector<int> &src, int *dst, int dstLen) {
	int len = src.length();
	int threadNum = 4;
	future<void> futureList[threadNum];        // 定义一个future<void> 数组,用来存放async返回值,这里的void是异步函数的返回类型。
	
	for (int i = 0; i < threadNum; i++) {      // for循环对每个线程启动async处理
		futureList[i] = std::async(std::launch::async, [&](int start, int end) {   // policy是async,代表程序运行到这个地方,就已经开始运行后面的lamda表达式了 
			for (int j = start; j < end; j++) {
				dst.at(j) = src.at(j);
			} 
		}, i * len / threadNum, (i + 1) * len / threadNum);  // 后面这两个是lamda表达式的输入参数。
	}
	
	for (int i = 0; i < threadNum; i++) {
		futureList[i].wait();              // 等待所有线程结束
	}
	
	return;
}

参考链接

23、bind函数

typedef std::function<void(int, int)> FUN

class BindFunc{
	public: 
		void func(int start, int end) {
			cout<<start<<"--"<<end;
		}
		BindFunc() {
			m_func = std::bind(&BindFunc::func, this, placeholders::_1, placeholders::_2);
		}
	private:
		FUN m_func;
}

(未完待续)

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值