I'm using Qt and trying to execute a command using QProcess. However, I'm encountering a timeout issue where the process operation times out. I've set up my code as follows:
void HostDecoder::usingCmd_btn_clicked() {
QString program = "C:\\Windows\\system32\\cmd.exe";
QString hostname = ui->hostname->text();
qDebug() << "hostname:" << hostname;
QProcess qprocess;
qprocess.startDetached(program, QStringList() << "ping" << hostname);
if (qprocess.waitForStarted()) {
if (qprocess.waitForFinished()) {
QByteArray output = qprocess.readAllStandardOutput();
qDebug() << "command output:" << output;
// Handle the output
} else {
qDebug() << "Failed to execute the command.";
// Error handling
}
} else {
qDebug() << "Failed to start the command.";
// Error handling
}
}
The code outputs "Failed to start the command." and I'm not sure why. I suspect it may be due to not setting the working directory. How can I resolve this issue?
I have already tried setting the read channel using setReadChannel() and setReadChannelMode(), as well as setting the process channel mode to SeparateChannels. However, the issue still persists.
Any help or suggestions would be greatly appreciated! Thank you in advance.
Note: I'm looking for a solution specific to Qt and QProcess.
Please let me know if there's anything else I can provide to help clarify the issue. Thank you!