Using system command in Qt

How can I use a command systemwhere the command is written in QString?

how

QString command="chmod -R 777 /opt/QT/examples/code/TestGUI/Data";    
system(command);

When compiling, I get this error:

cannot convert β€˜QString’ to β€˜const char*’
  for argument β€˜1’ to β€˜int system(const char*)’

Can anyone suggest something?

+5
source share
6 answers

Use macro qPrintable()

system(qPrintable(command));

+11
source

You need to get the original character array from QString. Here is one way:

system(command.toStdString().c_str());
+7
source

Ankur Gupta , QProcess ( ):

int QProcess::execute ( const QString & program )

:

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

, setPermissions QFile

0

QString const char*.

UTF8, :

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

else, UTF8, :

command.toLatin1().constData() ;
system(my_command);

- , .

0
source

All Articles