QProcess在运行命令时超时

huangapple go评论136阅读模式
英文:

QProcess times out when running a command

问题

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!

英文:

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 = &quot;C:\\Windows\\system32\\cmd.exe&quot;;
    QString hostname = ui-&gt;hostname-&gt;text();
    qDebug() &lt;&lt; &quot;hostname:&quot; &lt;&lt; hostname;

    QProcess qprocess;
    qprocess.startDetached(program, QStringList() &lt;&lt; &quot;ping&quot; &lt;&lt; hostname);

    if (qprocess.waitForStarted()) {
        if (qprocess.waitForFinished()) {
            QByteArray output = qprocess.readAllStandardOutput();
            qDebug() &lt;&lt; &quot;command output:&quot; &lt;&lt; output;
            // Handle the output
        } else {
            qDebug() &lt;&lt; &quot;Failed to execute the command.&quot;;
            // Error handling
        }
    } else {
        qDebug() &lt;&lt; &quot;Failed to start the command.&quot;;
        // 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!

答案1

得分: 1

startDetached 是静态函数,因此 waitForStarted 不适用,你在后台某处启动了一个 QProcess,然后想从另一个未初始化的对象中获取信息。

所以,只需使用 start 而不是 startDetached 在当前线程中运行外部进程。

英文:

startDetached is static function so waitForStarted is not applicable, you start one QProcess somewhere in background and then want to get info from the other, not so initialized object.

So just use start instead of startDetached to run external process in your current thread.

huangapple
  • 本文由 发表于 2023年7月4日 23:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定