View Full Version : how to run unix commands in qt creator in qt4.0
narlapavan
5th February 2012, 08:03
in qt3.3 i used to append QString directly to system command but in qt4.0 it is saying can't convert from QString to const char*
Example in QT 3.3
QString str ="ls -ltr";
system(str); // it is working in qt3.3, when i am running same statement in qt creator it giving can't convert from QString to const char*
any help???
Lykurg
5th February 2012, 08:41
Well, have you tried to convert QString to const char* yourself and then pass it to the function?
narlapavan
5th February 2012, 09:56
i tried that to its not wrking
Lykurg
5th February 2012, 11:09
So what have you tried? On my side manual conversion just works perfect.
narlapavan
6th February 2012, 14:45
QString str = "ls -ltr";
const char *cWrd = str;
system(cWrd);
ERROR::
error: cannot convert ‘QString’ to ‘const char*’ in initialization
Added after 9 minutes:
ok thanks for ur interest i found how to convert
QString str= "ls -ltr";
const char *c = str.toLocal8Bit().data();
Lykurg
6th February 2012, 16:27
Yes, that's the function you need. But this can crash (not likely but possible) because toLocal8Bit() creates a temporary byte array. So better write:
QString str= "ls -ltr";
QByteArray ba = str.toLocal8Bit();
system(ba.data());
wysota
6th February 2012, 16:38
Or just:
system("ls -ltr");
or:
QByteArray ba("ls -ltr");
system(ba.constData());
There is no point in going through Unicode (which is what QString uses internally) if you don't need it.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.