Solved Qt + command Shell
-
Hi all... is there any way around that i can call and read the result of windows shell commands like "date /t" or "time /t" through Qt app
-
@gaurav118
read the docs about QProcess -
@gaurav118 Hi,
For "cmd" command on Windows, you have to putcmd /C
in front of your command, otherwise you will see an error (exit code -1 and empty output):QString result; process.start("cmd /c date /t") if(!process.waitForFinished(timeout)) //Error else result = process.readAllStandardOutput()
see this post: https://forum.qt.io/topic/41918/executing-commands-in-cmd-exe-using-qprocess/5
A better way to handle output is to use signals readyReadStandardOutput() and readyReadStandardError(), using waitForFinished() was simpler for the example.
-
@Gojir4 I am pretty new at Qt. Do we have any sample project doing the same?
-
@gaurav118 What is the final goal of your application ?
-
@gaurav118 said in Qt + command Shell:
@Gojir4 I am pretty new at Qt. Do we have any sample project doing the same?
If you are that new: just to check: are you aware that for the examples you give you do not need to run an OS command and parse the output? Qt/C++/whatever will have its own functions for giving you the current date/time.
-
@JonB Yeah i know.. it has QDateTime::currentDateTime.
-
@Gojir4 The idea is to control basic functionality of OS from app itself like IP configuration, Date Time setting, OS reboot, Backend application execution. I just need some sample code on how to use QProcess delicately handling all its exceptions and errors
-
@gaurav118
Yep, then you do needQProcess
. For "handling all its exceptions and errors", you will need to read carefully through all its methods/signals/etc., as there are dedicated functions for these. -
Thanx all... I was actually looking for something like this.. Anyhow my issue is resolved now, in case any one need help, simply refer to http://www.informit.com/articles/article.aspx?p=1405549&seqNum=5