system()函数的使用
system()中的参数是char*型的,需要在变量后面使用toLatin1()函数进行转换。
system(command04.toLatin1()); //形参类型是char* ,实参是QString,不匹配,所以需要进行类型转换
一、QT中使用脚本语言有以下三种方式(以linux的“ls”为例说明):
(1)QProcess:execute("ls");
(2)system("ls");
(3)QProcess *process = new QProcess();
process->start("ls");
说明:
1、以上3中方法都可以,但是前两种会阻塞进程,直到ls结束,才会进行后续的操作,而第三种不会阻塞进程,可以多任务运行。
当命令中存在管道(|)和重定向(>)时,只能使用第二种,第一种和第三种属于Qt自身的,不能识别这两种符号。
二、把QString转换为char*。
system()的使用过程中,命令语句是由多个变量拼接而成的,由于system()中的参数是char*型的,需要在变量后面使用toLatin1()函数进行转换。
示例如下:
QString command01 ="echo ";
QString command02= QString::number(number,10);
QString command03="> /sys/devices/platform/s3c24xx-pwm.1/pwm-backlight.0/backlight/pwm-backlight.0/brightness"
QString command04 = command01+command02+command03;
system(command04.toLatin1()); //形参类型是char* ,实参是QString,不匹配,所以需要进行类型转换
三、把char*转换为QString。
最为方便的做法是以一个QLatin1String对象作为参数,来调用QString的构造函数。
示例:
const char *cstr;
QString qstr= QString(QLatin1String(cstr));
————————————————
原文链接:https://blog.csdn.net/papership/article/details/102475722