在Qt中使用系统命令

在Qt中使用系统命令,qt,Qt,如果命令写在QString中,如何使用system命令 比如: 编译时,出现以下错误: cannot convert ‘QString’ to ‘const char*’ for argument ‘1’ to ‘int system(const char*)’ 有人能给我一些建议吗?QProcess类。这就是您需要的。您需要从QString获取原始字符数组。这里有一个方法: system(command.toStdString().c_str()); 使用宏 系统(qPrintable

如果命令写在QString中,如何使用
system
命令

比如:

编译时,出现以下错误:

cannot convert ‘QString’ to ‘const char*’
  for argument ‘1’ to ‘int system(const char*)’

有人能给我一些建议吗?

QProcess类。这就是您需要的。

您需要从QString获取原始字符数组。这里有一个方法:

system(command.toStdString().c_str());
使用宏


系统(qPrintable(命令))

Ankur Gupta写道,使用QProcess静态函数():

在你的情况下:

QProcess::execute ("chmod -R 777 /opt/QT/examples/code/TestGUI/Data");

要更改权限,可以使用QFile的setPermissions

您可以将
QString
转换为
const char*

如果字符串为UTF8格式,则可以使用:

const char* my_command = command.toUtf8().constData() ;
system(my_command);
command.toLatin1().constData() ;
system(my_command);
否则,如果字符串不是UTF8格式,则可以使用:

const char* my_command = command.toUtf8().constData() ;
system(my_command);
command.toLatin1().constData() ;
system(my_command);

在这种情况下,第二个是您想要的。

可能是Hey的副本,谢谢!我不知道
qPrintable
。一个人每天都能学到一些东西。